├── .gitignore ├── .reviewboardrc ├── .travis.yml ├── LICENSE ├── NOTICE ├── README.md ├── checkstyle.xml ├── pom.xml ├── twill-api ├── pom.xml └── src │ └── main │ └── java │ └── org │ └── apache │ └── twill │ ├── api │ ├── AbstractTwillRunnable.java │ ├── ClassAcceptor.java │ ├── Command.java │ ├── Configs.java │ ├── ElectionHandler.java │ ├── EventHandler.java │ ├── EventHandlerContext.java │ ├── EventHandlerSpecification.java │ ├── Hosts.java │ ├── LocalFile.java │ ├── Racks.java │ ├── ResourceReport.java │ ├── ResourceSpecification.java │ ├── RunId.java │ ├── RuntimeSpecification.java │ ├── SecureStore.java │ ├── SecureStoreUpdater.java │ ├── ServiceAnnouncer.java │ ├── ServiceController.java │ ├── TwillApplication.java │ ├── TwillContext.java │ ├── TwillController.java │ ├── TwillPreparer.java │ ├── TwillRunResources.java │ ├── TwillRunnable.java │ ├── TwillRunnableSpecification.java │ ├── TwillRunner.java │ ├── TwillRunnerService.java │ ├── TwillSpecification.java │ ├── logging │ │ ├── LogEntry.java │ │ ├── LogHandler.java │ │ ├── LogThrowable.java │ │ ├── PrinterLogHandler.java │ │ └── package-info.java │ ├── package-info.java │ └── security │ │ ├── SecureStoreRenewer.java │ │ └── SecureStoreWriter.java │ └── internal │ ├── DefaultEventHandlerSpecification.java │ ├── DefaultLocalFile.java │ ├── DefaultResourceSpecification.java │ ├── DefaultRuntimeSpecification.java │ ├── DefaultTwillRunResources.java │ ├── DefaultTwillRunnableSpecification.java │ ├── DefaultTwillSpecification.java │ ├── RunIds.java │ └── package-info.java ├── twill-common ├── pom.xml └── src │ └── main │ └── java │ └── org │ └── apache │ └── twill │ ├── common │ ├── Cancellable.java │ └── Threads.java │ ├── filesystem │ ├── ForwardingLocationFactory.java │ ├── LocalLocation.java │ ├── LocalLocationFactory.java │ ├── Location.java │ ├── LocationFactories.java │ └── LocationFactory.java │ └── internal │ └── Constants.java ├── twill-core ├── pom.xml └── src │ ├── main │ └── java │ │ └── org │ │ └── apache │ │ └── twill │ │ ├── internal │ │ ├── AbstractExecutionServiceController.java │ │ ├── AbstractTwillController.java │ │ ├── AbstractTwillService.java │ │ ├── AbstractZKServiceController.java │ │ ├── ApplicationBundler.java │ │ ├── Arguments.java │ │ ├── BasicTwillContext.java │ │ ├── CompositeService.java │ │ ├── ContainerInfo.java │ │ ├── ContainerLiveNodeData.java │ │ ├── DefaultResourceReport.java │ │ ├── ElectionRegistry.java │ │ ├── EnvContainerInfo.java │ │ ├── EnvKeys.java │ │ ├── JvmOptions.java │ │ ├── ListenerExecutor.java │ │ ├── LogOnlyEventHandler.java │ │ ├── ProcessController.java │ │ ├── ProcessLauncher.java │ │ ├── ResourceCapability.java │ │ ├── ServiceListenerAdapter.java │ │ ├── Services.java │ │ ├── SingleRunnableApplication.java │ │ ├── TwillContainerController.java │ │ ├── TwillContainerLauncher.java │ │ ├── TwillRuntimeSpecification.java │ │ ├── ZKMessages.java │ │ ├── io │ │ │ ├── BasicLocationCache.java │ │ │ ├── LocationCache.java │ │ │ └── NoCachingLocationCache.java │ │ ├── json │ │ │ ├── ArgumentsCodec.java │ │ │ ├── DefaultLogThrowable.java │ │ │ ├── ILoggingEventSerializer.java │ │ │ ├── JsonUtils.java │ │ │ ├── LocalFileCodec.java │ │ │ ├── LogEntryDecoder.java │ │ │ ├── LogThrowableCodec.java │ │ │ ├── ResourceReportAdapter.java │ │ │ ├── ResourceReportCodec.java │ │ │ ├── ResourceSpecificationCodec.java │ │ │ ├── RuntimeSpecificationCodec.java │ │ │ ├── StackTraceElementCodec.java │ │ │ ├── TwillRunResourcesCodec.java │ │ │ ├── TwillRunnableSpecificationCodec.java │ │ │ ├── TwillRuntimeSpecificationAdapter.java │ │ │ ├── TwillRuntimeSpecificationCodec.java │ │ │ └── TwillSpecificationCodec.java │ │ ├── kafka │ │ │ ├── EmbeddedKafkaServer.java │ │ │ └── client │ │ │ │ ├── BasicFetchedMessage.java │ │ │ │ ├── ByteBufferEncoder.java │ │ │ │ ├── IntegerEncoder.java │ │ │ │ ├── SimpleKafkaConsumer.java │ │ │ │ ├── SimpleKafkaPublisher.java │ │ │ │ ├── ZKBrokerService.java │ │ │ │ ├── ZKKafkaClientService.java │ │ │ │ └── package-info.java │ │ ├── logging │ │ │ ├── KafkaAppender.java │ │ │ └── Loggings.java │ │ ├── package-info.java │ │ ├── state │ │ │ ├── Message.java │ │ │ ├── MessageCallback.java │ │ │ ├── MessageCodec.java │ │ │ ├── Messages.java │ │ │ ├── SimpleMessage.java │ │ │ └── SystemMessages.java │ │ └── utils │ │ │ ├── Dependencies.java │ │ │ ├── Instances.java │ │ │ ├── Networks.java │ │ │ ├── Paths.java │ │ │ └── Resources.java │ │ ├── kafka │ │ └── client │ │ │ ├── BrokerInfo.java │ │ │ ├── BrokerService.java │ │ │ ├── Compression.java │ │ │ ├── FetchedMessage.java │ │ │ ├── KafkaClient.java │ │ │ ├── KafkaClientService.java │ │ │ ├── KafkaConsumer.java │ │ │ ├── KafkaPublisher.java │ │ │ ├── TopicPartition.java │ │ │ └── package-info.java │ │ └── launcher │ │ ├── FindFreePort.java │ │ └── TwillLauncher.java │ └── test │ ├── java │ └── org │ │ └── apache │ │ └── twill │ │ ├── internal │ │ ├── CompositeServiceTest.java │ │ ├── ControllerTest.java │ │ ├── DebugOptionsTest.java │ │ ├── ServicesTest.java │ │ ├── json │ │ │ └── ResourceSpecificationCodecTest.java │ │ ├── state │ │ │ └── MessageCodecTest.java │ │ └── utils │ │ │ ├── ApplicationBundlerTest.java │ │ │ ├── Class1.java │ │ │ └── Class2.java │ │ └── kafka │ │ └── client │ │ └── KafkaTest.java │ └── resources │ └── logback-test.xml ├── twill-discovery-api ├── pom.xml └── src │ ├── main │ └── java │ │ └── org │ │ └── apache │ │ └── twill │ │ └── discovery │ │ ├── Discoverable.java │ │ ├── DiscoveryService.java │ │ ├── DiscoveryServiceClient.java │ │ └── ServiceDiscovered.java │ └── test │ └── java │ └── org │ └── apache │ └── twill │ └── discovery │ └── DiscoverableTest.java ├── twill-discovery-core ├── pom.xml └── src │ ├── main │ └── java │ │ └── org │ │ └── apache │ │ └── twill │ │ └── discovery │ │ ├── DefaultServiceDiscovered.java │ │ ├── DiscoverableAdapter.java │ │ ├── InMemoryDiscoveryService.java │ │ ├── ZKDiscoveryService.java │ │ └── package-info.java │ └── test │ ├── java │ └── org │ │ └── apache │ │ └── twill │ │ └── discovery │ │ ├── DiscoveryServiceTestBase.java │ │ ├── InMemoryDiscoveryServiceTest.java │ │ └── ZKDiscoveryServiceTest.java │ └── resources │ └── logback-test.xml ├── twill-examples ├── echo │ ├── pom.xml │ └── src │ │ └── main │ │ └── java │ │ └── echo │ │ └── EchoMain.java ├── pom.xml └── yarn │ ├── pom.xml │ └── src │ └── main │ └── java │ └── org │ └── apache │ └── twill │ └── example │ └── yarn │ ├── BundledJarExample.java │ └── HelloWorld.java ├── twill-ext ├── pom.xml └── src │ ├── main │ └── java │ │ └── org │ │ └── apache │ │ └── twill │ │ └── ext │ │ ├── BundledJarRunnable.java │ │ └── BundledJarRunner.java │ └── test │ └── java │ └── org │ └── apache │ └── twill │ └── ext │ ├── BundledJarRunnableTest.java │ └── BundledJarRunnerTest.java ├── twill-java8-test ├── pom.xml └── src │ └── test │ └── java │ └── org │ └── apache │ └── twill │ └── test │ └── Java8Test.java ├── twill-yarn ├── pom.xml └── src │ ├── main │ ├── hadoop21 │ │ └── org │ │ │ └── apache │ │ │ └── twill │ │ │ └── internal │ │ │ └── yarn │ │ │ ├── Hadoop21YarnAMClient.java │ │ │ ├── Hadoop21YarnAppClient.java │ │ │ ├── Hadoop21YarnApplicationReport.java │ │ │ ├── Hadoop21YarnContainerInfo.java │ │ │ ├── Hadoop21YarnContainerStatus.java │ │ │ ├── Hadoop21YarnLaunchContext.java │ │ │ ├── Hadoop21YarnLocalResource.java │ │ │ └── Hadoop21YarnNMClient.java │ ├── hadoop22 │ │ └── org │ │ │ └── apache │ │ │ └── twill │ │ │ └── internal │ │ │ └── yarn │ │ │ └── Hadoop22YarnAMClient.java │ ├── hadoop23 │ │ └── org │ │ │ └── apache │ │ │ └── twill │ │ │ └── internal │ │ │ └── yarn │ │ │ └── Hadoop23YarnAppClient.java │ ├── hadoop26 │ │ └── org │ │ │ └── apache │ │ │ └── twill │ │ │ └── internal │ │ │ └── yarn │ │ │ ├── Hadoop26YarnAMClient.java │ │ │ └── Hadoop26YarnAppClient.java │ ├── java │ │ └── org │ │ │ └── apache │ │ │ └── twill │ │ │ ├── filesystem │ │ │ ├── FileContextLocation.java │ │ │ ├── FileContextLocationFactory.java │ │ │ ├── FileContextLocationUtil.java │ │ │ └── package-info.java │ │ │ ├── internal │ │ │ ├── ContainerExitCodes.java │ │ │ ├── ServiceMain.java │ │ │ ├── appmaster │ │ │ │ ├── AllocationSpecification.java │ │ │ │ ├── ApplicationMasterInfo.java │ │ │ │ ├── ApplicationMasterLiveNodeData.java │ │ │ │ ├── ApplicationMasterMain.java │ │ │ │ ├── ApplicationMasterProcessLauncher.java │ │ │ │ ├── ApplicationMasterService.java │ │ │ │ ├── ApplicationSubmitter.java │ │ │ │ ├── BasicEventHandlerContext.java │ │ │ │ ├── ExpectedContainers.java │ │ │ │ ├── LoggerContextListenerAdapter.java │ │ │ │ ├── PlacementPolicyManager.java │ │ │ │ ├── ProvisionRequest.java │ │ │ │ ├── RunnableContainerRequest.java │ │ │ │ ├── RunnableProcessLauncher.java │ │ │ │ ├── RunningContainers.java │ │ │ │ ├── TrackerService.java │ │ │ │ └── package-info.java │ │ │ ├── container │ │ │ │ ├── TwillContainerMain.java │ │ │ │ └── TwillContainerService.java │ │ │ └── yarn │ │ │ │ ├── AbstractYarnAMClient.java │ │ │ │ ├── AbstractYarnProcessLauncher.java │ │ │ │ ├── AbstractYarnTwillService.java │ │ │ │ ├── VersionDetectYarnAMClientFactory.java │ │ │ │ ├── VersionDetectYarnAppClientFactory.java │ │ │ │ ├── YarnAMClient.java │ │ │ │ ├── YarnAMClientFactory.java │ │ │ │ ├── YarnAppClient.java │ │ │ │ ├── YarnAppClientFactory.java │ │ │ │ ├── YarnApplicationReport.java │ │ │ │ ├── YarnContainerInfo.java │ │ │ │ ├── YarnContainerStatus.java │ │ │ │ ├── YarnLaunchContext.java │ │ │ │ ├── YarnLocalResource.java │ │ │ │ ├── YarnNMClient.java │ │ │ │ ├── YarnUtils.java │ │ │ │ └── package-info.java │ │ │ └── yarn │ │ │ ├── ApplicationMasterLiveNodeDecoder.java │ │ │ ├── LocationCacheCleaner.java │ │ │ ├── LocationSecureStoreRenewer.java │ │ │ ├── ResourceReportClient.java │ │ │ ├── YarnSecureStore.java │ │ │ ├── YarnTwillController.java │ │ │ ├── YarnTwillControllerFactory.java │ │ │ ├── YarnTwillPreparer.java │ │ │ ├── YarnTwillRunnerService.java │ │ │ └── package-info.java │ └── resources │ │ └── logback-template.xml │ └── test │ ├── java │ └── org │ │ └── apache │ │ └── twill │ │ ├── filesystem │ │ ├── FileContextLocationTest.java │ │ ├── LocalLocationTest.java │ │ └── LocationTestBase.java │ │ └── yarn │ │ ├── AppRecoveryTestRun.java │ │ ├── BaseYarnTest.java │ │ ├── BuggyServer.java │ │ ├── ContainerSizeTestRun.java │ │ ├── CustomClassLoader.java │ │ ├── CustomClassLoaderRunnable.java │ │ ├── CustomClassLoaderTestRun.java │ │ ├── DebugTestRun.java │ │ ├── DistributeShellTestRun.java │ │ ├── DistributedShell.java │ │ ├── EchoServer.java │ │ ├── EchoServerTestRun.java │ │ ├── EnvironmentEchoServer.java │ │ ├── EnvironmentTestRun.java │ │ ├── EventHandlerTestRun.java │ │ ├── FailureRestartTestRun.java │ │ ├── InitializeFailTestRun.java │ │ ├── JvmOptionsTestRun.java │ │ ├── LocalFileTestRun.java │ │ ├── LocationCacheCleanerTest.java │ │ ├── LocationCacheTest.java │ │ ├── LogHandlerTestRun.java │ │ ├── LogLevelChangeTestRun.java │ │ ├── LogLevelTestRun.java │ │ ├── MaxRetriesTestRun.java │ │ ├── PlacementPolicyTestRun.java │ │ ├── ProvisionTimeoutTestRun.java │ │ ├── ResourceReportTestRun.java │ │ ├── RestartRunnableTestRun.java │ │ ├── ServiceDiscoveryTestRun.java │ │ ├── SessionExpireTestRun.java │ │ ├── SocketServer.java │ │ ├── TaskCompletedTestRun.java │ │ ├── TwillSpecificationTest.java │ │ ├── TwillTester.java │ │ └── YarnTestSuite.java │ └── resources │ └── logback-test.xml └── twill-zookeeper ├── pom.xml └── src ├── main └── java │ └── org │ └── apache │ └── twill │ ├── internal │ └── zookeeper │ │ ├── BasicACLData.java │ │ ├── BasicNodeChildren.java │ │ ├── BasicNodeData.java │ │ ├── DefaultZKClientService.java │ │ ├── FailureRetryZKClient.java │ │ ├── InMemoryZKServer.java │ │ ├── KillZKSession.java │ │ ├── LeaderElection.java │ │ ├── NamespaceZKClient.java │ │ ├── ReentrantDistributedLock.java │ │ ├── RetryUtils.java │ │ ├── RewatchOnExpireWatcher.java │ │ ├── RewatchOnExpireZKClient.java │ │ ├── SettableOperationFuture.java │ │ └── package-info.java │ └── zookeeper │ ├── ACLData.java │ ├── AbstractZKClient.java │ ├── ForwardingZKClient.java │ ├── ForwardingZKClientService.java │ ├── NodeChildren.java │ ├── NodeData.java │ ├── OperationFuture.java │ ├── RetryStrategies.java │ ├── RetryStrategy.java │ ├── ZKClient.java │ ├── ZKClientService.java │ ├── ZKClientServices.java │ ├── ZKClients.java │ ├── ZKOperations.java │ └── package-info.java └── test ├── java └── org │ └── apache │ └── twill │ ├── internal │ └── zookeeper │ │ ├── LeaderElectionTest.java │ │ └── ReentrantDistributedLockTest.java │ └── zookeeper │ ├── RetryStrategyTest.java │ ├── ZKClientTest.java │ └── ZKOperationsTest.java └── resources └── logback-test.xml /.gitignore: -------------------------------------------------------------------------------- 1 | *.class 2 | .*.swp 3 | 4 | # Package Files # 5 | *.jar 6 | *.war 7 | *.ear 8 | 9 | # Intellij Files & Dir # 10 | *.iml 11 | *.ipr 12 | *.iws 13 | atlassian-ide-plugin.xml 14 | out/ 15 | .DS_Store 16 | lib/ 17 | .idea 18 | 19 | # Eclipse Files and Dir # 20 | .project 21 | .classpath 22 | .settings 23 | 24 | # Gradle Files & Dir # 25 | build/ 26 | .stickyStorage 27 | .build/ 28 | target/ 29 | 30 | # relics of test runs 31 | logs/ 32 | zookeeper.out 33 | 34 | /bin 35 | -------------------------------------------------------------------------------- /.reviewboardrc: -------------------------------------------------------------------------------- 1 | # 2 | # Licensed to the Apache Software Foundation (ASF) under one 3 | # or more contributor license agreements. See the NOTICE file 4 | # distributed with this work for additional information 5 | # regarding copyright ownership. The ASF licenses this file 6 | # to you under the Apache License, Version 2.0 (the 7 | # "License"); you may not use this file except in compliance 8 | # with the License. You may obtain a copy of the License at 9 | # 10 | # http://www.apache.org/licenses/LICENSE-2.0 11 | # 12 | # Unless required by applicable law or agreed to in writing, software 13 | # distributed under the License is distributed on an "AS IS" BASIS, 14 | # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 15 | # See the License for the specific language governing permissions and 16 | # limitations under the License. 17 | # 18 | 19 | REPOSITORY = "twill" 20 | REVIEWBOARD_URL = "https://reviews.apache.org" 21 | TARGET_GROUPS = "Twill" 22 | -------------------------------------------------------------------------------- /.travis.yml: -------------------------------------------------------------------------------- 1 | # 2 | # Licensed to the Apache Software Foundation (ASF) under one 3 | # or more contributor license agreements. See the NOTICE file 4 | # distributed with this work for additional information 5 | # regarding copyright ownership. The ASF licenses this file 6 | # to you under the Apache License, Version 2.0 (the 7 | # "License"); you may not use this file except in compliance 8 | # with the License. You may obtain a copy of the License at 9 | # 10 | # http://www.apache.org/licenses/LICENSE-2.0 11 | # 12 | # Unless required by applicable law or agreed to in writing, software 13 | # distributed under the License is distributed on an "AS IS" BASIS, 14 | # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 15 | # See the License for the specific language governing permissions and 16 | # limitations under the License. 17 | # 18 | 19 | language: java 20 | 21 | jdk: 22 | - openjdk8 23 | 24 | branches: 25 | only: 26 | - master 27 | - /^branch\-.*$/ 28 | - /^feature\/.*$/ 29 | 30 | script: mvn --batch-mode test -P $PROFILE -Dsurefire.redirectTestOutputToFile=false 31 | 32 | install: mvn --batch-mode install -P $PROFILE -DskipTests=true 33 | 34 | env: 35 | - PROFILE='hadoop-2.1' 36 | - PROFILE='hadoop-2.2' 37 | - PROFILE='hadoop-2.4' 38 | - PROFILE='hadoop-2.5' 39 | - PROFILE='hadoop-2.6' 40 | - PROFILE='mapr-hadoop-2.4' 41 | - PROFILE='hadoop-2.6,java8-test' 42 | 43 | sudo: false 44 | 45 | cache: 46 | directories: 47 | - $HOME/.m2 48 | -------------------------------------------------------------------------------- /NOTICE: -------------------------------------------------------------------------------- 1 | Apache Twill 2 | Copyright 2013-2017 The Apache Software Foundation 3 | 4 | This product includes software developed at 5 | The Apache Software Foundation (http://www.apache.org/). 6 | -------------------------------------------------------------------------------- /twill-api/pom.xml: -------------------------------------------------------------------------------- 1 | 2 | 19 | 22 | 4.0.0 23 | 24 | 25 | org.apache.twill 26 | twill-parent 27 | 0.15.0-SNAPSHOT 28 | 29 | 30 | twill-api 31 | jar 32 | Apache Twill API 33 | 34 | 35 | 36 | ${project.groupId} 37 | twill-common 38 | ${project.version} 39 | 40 | 41 | ${project.groupId} 42 | twill-discovery-api 43 | ${project.version} 44 | 45 | 46 | com.google.code.findbugs 47 | jsr305 48 | 49 | 50 | 51 | -------------------------------------------------------------------------------- /twill-api/src/main/java/org/apache/twill/api/ClassAcceptor.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Licensed to the Apache Software Foundation (ASF) under one 3 | * or more contributor license agreements. See the NOTICE file 4 | * distributed with this work for additional information 5 | * regarding copyright ownership. The ASF licenses this file 6 | * to you under the Apache License, Version 2.0 (the 7 | * "License"); you may not use this file except in compliance 8 | * with the License. You may obtain a copy of the License at 9 | * 10 | * http://www.apache.org/licenses/LICENSE-2.0 11 | * 12 | * Unless required by applicable law or agreed to in writing, software 13 | * distributed under the License is distributed on an "AS IS" BASIS, 14 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 15 | * See the License for the specific language governing permissions and 16 | * limitations under the License. 17 | */ 18 | package org.apache.twill.api; 19 | 20 | import java.net.URL; 21 | 22 | /** 23 | * Class that can be used to determine if class can be accepted. 24 | */ 25 | public class ClassAcceptor { 26 | /** 27 | * Invoked to determine if class can be accepted. default behavior returns true. 28 | * 29 | * @param className Name of the class. 30 | * @param classUrl URL for the class resource. 31 | * @param classPathUrl URL for the class path resource that contains the class resource. 32 | * If the URL protocol is {@code file}, it would be the path to root package. 33 | * If the URL protocol is {@code jar}, it would be the jar file. 34 | * @return true to accept the given class, false otherwise. 35 | */ 36 | public boolean accept(String className, URL classUrl, URL classPathUrl) { 37 | return true; 38 | } 39 | } 40 | -------------------------------------------------------------------------------- /twill-api/src/main/java/org/apache/twill/api/ElectionHandler.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Licensed to the Apache Software Foundation (ASF) under one 3 | * or more contributor license agreements. See the NOTICE file 4 | * distributed with this work for additional information 5 | * regarding copyright ownership. The ASF licenses this file 6 | * to you under the Apache License, Version 2.0 (the 7 | * "License"); you may not use this file except in compliance 8 | * with the License. You may obtain a copy of the License at 9 | * 10 | * http://www.apache.org/licenses/LICENSE-2.0 11 | * 12 | * Unless required by applicable law or agreed to in writing, software 13 | * distributed under the License is distributed on an "AS IS" BASIS, 14 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 15 | * See the License for the specific language governing permissions and 16 | * limitations under the License. 17 | */ 18 | package org.apache.twill.api; 19 | 20 | /** 21 | * Handles events of election/un-election of leader. 22 | */ 23 | public interface ElectionHandler { 24 | 25 | /** 26 | * This method will get invoked when a participant becomes a leader in a 27 | * leader election process. It is guaranteed that this method won't get called 28 | * consecutively (i.e. called twice or more in a row). 29 | */ 30 | void leader(); 31 | 32 | /** 33 | * This method will get invoked when a participant is a follower in a 34 | * leader election process. This method might get called multiple times without 35 | * the {@link #leader()} method being called. 36 | */ 37 | void follower(); 38 | } 39 | -------------------------------------------------------------------------------- /twill-api/src/main/java/org/apache/twill/api/EventHandlerContext.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Licensed to the Apache Software Foundation (ASF) under one 3 | * or more contributor license agreements. See the NOTICE file 4 | * distributed with this work for additional information 5 | * regarding copyright ownership. The ASF licenses this file 6 | * to you under the Apache License, Version 2.0 (the 7 | * "License"); you may not use this file except in compliance 8 | * with the License. You may obtain a copy of the License at 9 | * 10 | * http://www.apache.org/licenses/LICENSE-2.0 11 | * 12 | * Unless required by applicable law or agreed to in writing, software 13 | * distributed under the License is distributed on an "AS IS" BASIS, 14 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 15 | * See the License for the specific language governing permissions and 16 | * limitations under the License. 17 | */ 18 | package org.apache.twill.api; 19 | 20 | /** 21 | * Represents runtime context for {@link EventHandler}. 22 | */ 23 | public interface EventHandlerContext { 24 | 25 | /** 26 | * @return Name of the {@link TwillApplication} containing the {@link EventHandler} 27 | */ 28 | String getApplicationName(); 29 | 30 | /** 31 | * @return the unique ID of the current run of the {@link TwillApplication} containing the {@link EventHandler} 32 | */ 33 | RunId getRunId(); 34 | 35 | /** 36 | * @return {@link EventHandlerSpecification} of the {@link EventHandler} 37 | */ 38 | EventHandlerSpecification getSpecification(); 39 | } 40 | -------------------------------------------------------------------------------- /twill-api/src/main/java/org/apache/twill/api/EventHandlerSpecification.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Licensed to the Apache Software Foundation (ASF) under one 3 | * or more contributor license agreements. See the NOTICE file 4 | * distributed with this work for additional information 5 | * regarding copyright ownership. The ASF licenses this file 6 | * to you under the Apache License, Version 2.0 (the 7 | * "License"); you may not use this file except in compliance 8 | * with the License. You may obtain a copy of the License at 9 | * 10 | * http://www.apache.org/licenses/LICENSE-2.0 11 | * 12 | * Unless required by applicable law or agreed to in writing, software 13 | * distributed under the License is distributed on an "AS IS" BASIS, 14 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 15 | * See the License for the specific language governing permissions and 16 | * limitations under the License. 17 | */ 18 | package org.apache.twill.api; 19 | 20 | import java.util.Map; 21 | 22 | /** 23 | * Specification for {@link EventHandler}. 24 | */ 25 | public interface EventHandlerSpecification { 26 | 27 | String getClassName(); 28 | 29 | Map getConfigs(); 30 | } 31 | -------------------------------------------------------------------------------- /twill-api/src/main/java/org/apache/twill/api/Hosts.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Licensed to the Apache Software Foundation (ASF) under one 3 | * or more contributor license agreements. See the NOTICE file 4 | * distributed with this work for additional information 5 | * regarding copyright ownership. The ASF licenses this file 6 | * to you under the Apache License, Version 2.0 (the 7 | * "License"); you may not use this file except in compliance 8 | * with the License. You may obtain a copy of the License at 9 | * 10 | * http://www.apache.org/licenses/LICENSE-2.0 11 | * 12 | * Unless required by applicable law or agreed to in writing, software 13 | * distributed under the License is distributed on an "AS IS" BASIS, 14 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 15 | * See the License for the specific language governing permissions and 16 | * limitations under the License. 17 | */ 18 | package org.apache.twill.api; 19 | 20 | import java.util.Arrays; 21 | import java.util.Collections; 22 | import java.util.HashSet; 23 | import java.util.Set; 24 | 25 | /** 26 | * Represents a list of hosts. 27 | */ 28 | 29 | public class Hosts { 30 | private final Set hosts; 31 | 32 | public Hosts(Set hosts) { 33 | this.hosts = Collections.unmodifiableSet(new HashSet(hosts)); 34 | } 35 | 36 | public Hosts(String host, String...moreHosts) { 37 | Set hosts = new HashSet(); 38 | hosts.add(host); 39 | hosts.addAll(Arrays.asList(moreHosts)); 40 | 41 | this.hosts = Collections.unmodifiableSet(hosts); 42 | } 43 | 44 | /** 45 | * Convenience method to create an instance of {@link org.apache.twill.api.Hosts}. 46 | * @param host A host to be added. 47 | * @param moreHosts A list of hosts to be added. 48 | * @return An instance of {@link org.apache.twill.api.Hosts} containing specified hosts. 49 | */ 50 | public static Hosts of(String host, String...moreHosts) { 51 | return new Hosts(host, moreHosts); 52 | } 53 | 54 | /** 55 | * Get the list of hosts. 56 | * @return list of hosts. 57 | */ 58 | public Set get() { 59 | return this.hosts; 60 | } 61 | 62 | @Override 63 | public String toString() { 64 | return this.hosts.toString(); 65 | } 66 | } 67 | -------------------------------------------------------------------------------- /twill-api/src/main/java/org/apache/twill/api/LocalFile.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Licensed to the Apache Software Foundation (ASF) under one 3 | * or more contributor license agreements. See the NOTICE file 4 | * distributed with this work for additional information 5 | * regarding copyright ownership. The ASF licenses this file 6 | * to you under the Apache License, Version 2.0 (the 7 | * "License"); you may not use this file except in compliance 8 | * with the License. You may obtain a copy of the License at 9 | * 10 | * http://www.apache.org/licenses/LICENSE-2.0 11 | * 12 | * Unless required by applicable law or agreed to in writing, software 13 | * distributed under the License is distributed on an "AS IS" BASIS, 14 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 15 | * See the License for the specific language governing permissions and 16 | * limitations under the License. 17 | */ 18 | package org.apache.twill.api; 19 | 20 | import java.net.URI; 21 | import javax.annotation.Nullable; 22 | 23 | /** 24 | * This interface represents a local file that will be available for the container running a {@link TwillRunnable}. 25 | */ 26 | public interface LocalFile { 27 | 28 | String getName(); 29 | 30 | URI getURI(); 31 | 32 | /** 33 | * Returns the the last modified time of the file or {@code -1} if unknown. 34 | */ 35 | long getLastModified(); 36 | 37 | /** 38 | * Returns the size of the file or {@code -1} if unknown. 39 | */ 40 | long getSize(); 41 | 42 | /** 43 | * Indicates whether this file is an archive. If true, the file is expanded after being copied to the container host. 44 | */ 45 | boolean isArchive(); 46 | 47 | @Nullable 48 | String getPattern(); 49 | } 50 | -------------------------------------------------------------------------------- /twill-api/src/main/java/org/apache/twill/api/Racks.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Licensed to the Apache Software Foundation (ASF) under one 3 | * or more contributor license agreements. See the NOTICE file 4 | * distributed with this work for additional information 5 | * regarding copyright ownership. The ASF licenses this file 6 | * to you under the Apache License, Version 2.0 (the 7 | * "License"); you may not use this file except in compliance 8 | * with the License. You may obtain a copy of the License at 9 | * 10 | * http://www.apache.org/licenses/LICENSE-2.0 11 | * 12 | * Unless required by applicable law or agreed to in writing, software 13 | * distributed under the License is distributed on an "AS IS" BASIS, 14 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 15 | * See the License for the specific language governing permissions and 16 | * limitations under the License. 17 | */ 18 | package org.apache.twill.api; 19 | 20 | import java.util.Arrays; 21 | import java.util.Collections; 22 | import java.util.HashSet; 23 | import java.util.Set; 24 | 25 | /** 26 | * Represents a list of Racks. 27 | */ 28 | 29 | public class Racks { 30 | private final Set racks; 31 | 32 | public Racks(Set racks) { 33 | this.racks = Collections.unmodifiableSet(new HashSet(racks)); 34 | } 35 | 36 | public Racks(String rack, String...moreRacks) { 37 | Set racks = new HashSet(); 38 | racks.add(rack); 39 | racks.addAll(Arrays.asList(moreRacks)); 40 | 41 | this.racks = Collections.unmodifiableSet(racks); 42 | 43 | } 44 | 45 | /** 46 | * Convenience method to create an instance of {@link org.apache.twill.api.Racks}. 47 | * @param rack A rack to be added. 48 | * @param moreRacks A list of racks to be added. 49 | * @return An instance of {@link org.apache.twill.api.Racks} containing specified racks. 50 | */ 51 | public static Racks of(String rack, String...moreRacks) { 52 | return new Racks(rack, moreRacks); 53 | } 54 | 55 | /** 56 | * Get the list of racks. 57 | * @return list of racks. 58 | */ 59 | public Set get() { 60 | return this.racks; 61 | } 62 | 63 | @Override 64 | public String toString() { 65 | return this.racks.toString(); 66 | } 67 | } 68 | -------------------------------------------------------------------------------- /twill-api/src/main/java/org/apache/twill/api/ResourceReport.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Licensed to the Apache Software Foundation (ASF) under one 3 | * or more contributor license agreements. See the NOTICE file 4 | * distributed with this work for additional information 5 | * regarding copyright ownership. The ASF licenses this file 6 | * to you under the Apache License, Version 2.0 (the 7 | * "License"); you may not use this file except in compliance 8 | * with the License. You may obtain a copy of the License at 9 | * 10 | * http://www.apache.org/licenses/LICENSE-2.0 11 | * 12 | * Unless required by applicable law or agreed to in writing, software 13 | * distributed under the License is distributed on an "AS IS" BASIS, 14 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 15 | * See the License for the specific language governing permissions and 16 | * limitations under the License. 17 | */ 18 | package org.apache.twill.api; 19 | 20 | import java.util.Collection; 21 | import java.util.List; 22 | import java.util.Map; 23 | 24 | /** 25 | * This interface provides a snapshot of the resources an application is using 26 | * broken down by each runnable. 27 | */ 28 | public interface ResourceReport { 29 | /** 30 | * Get all the run resources being used by all instances of the specified runnable. 31 | * 32 | * @param runnableName the runnable name. 33 | * @return resources being used by all instances of the runnable. 34 | */ 35 | Collection getRunnableResources(String runnableName); 36 | 37 | /** 38 | * Get all the run resources being used across all runnables. 39 | * 40 | * @return all run resources used by all instances of all runnables. 41 | */ 42 | Map> getResources(); 43 | 44 | /** 45 | * Get the resources application master is using. 46 | * 47 | * @return resources being used by the application master. 48 | */ 49 | TwillRunResources getAppMasterResources(); 50 | 51 | /** 52 | * Get the id of the application master. 53 | * 54 | * @return id of the application master. 55 | */ 56 | String getApplicationId(); 57 | 58 | /** 59 | * Get the list of services of the application master. 60 | * 61 | * @return list of services of the application master. 62 | */ 63 | List getServices(); 64 | } 65 | -------------------------------------------------------------------------------- /twill-api/src/main/java/org/apache/twill/api/RunId.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Licensed to the Apache Software Foundation (ASF) under one 3 | * or more contributor license agreements. See the NOTICE file 4 | * distributed with this work for additional information 5 | * regarding copyright ownership. The ASF licenses this file 6 | * to you under the Apache License, Version 2.0 (the 7 | * "License"); you may not use this file except in compliance 8 | * with the License. You may obtain a copy of the License at 9 | * 10 | * http://www.apache.org/licenses/LICENSE-2.0 11 | * 12 | * Unless required by applicable law or agreed to in writing, software 13 | * distributed under the License is distributed on an "AS IS" BASIS, 14 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 15 | * See the License for the specific language governing permissions and 16 | * limitations under the License. 17 | */ 18 | package org.apache.twill.api; 19 | 20 | /** 21 | * Represents the unique ID of a particular execution. 22 | */ 23 | public interface RunId { 24 | 25 | String getId(); 26 | } 27 | -------------------------------------------------------------------------------- /twill-api/src/main/java/org/apache/twill/api/RuntimeSpecification.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Licensed to the Apache Software Foundation (ASF) under one 3 | * or more contributor license agreements. See the NOTICE file 4 | * distributed with this work for additional information 5 | * regarding copyright ownership. The ASF licenses this file 6 | * to you under the Apache License, Version 2.0 (the 7 | * "License"); you may not use this file except in compliance 8 | * with the License. You may obtain a copy of the License at 9 | * 10 | * http://www.apache.org/licenses/LICENSE-2.0 11 | * 12 | * Unless required by applicable law or agreed to in writing, software 13 | * distributed under the License is distributed on an "AS IS" BASIS, 14 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 15 | * See the License for the specific language governing permissions and 16 | * limitations under the License. 17 | */ 18 | package org.apache.twill.api; 19 | 20 | import java.util.Collection; 21 | 22 | /** 23 | * Specifications for runtime requirements. 24 | */ 25 | public interface RuntimeSpecification { 26 | 27 | String getName(); 28 | 29 | TwillRunnableSpecification getRunnableSpecification(); 30 | 31 | ResourceSpecification getResourceSpecification(); 32 | 33 | Collection getLocalFiles(); 34 | } 35 | -------------------------------------------------------------------------------- /twill-api/src/main/java/org/apache/twill/api/SecureStore.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Licensed to the Apache Software Foundation (ASF) under one 3 | * or more contributor license agreements. See the NOTICE file 4 | * distributed with this work for additional information 5 | * regarding copyright ownership. The ASF licenses this file 6 | * to you under the Apache License, Version 2.0 (the 7 | * "License"); you may not use this file except in compliance 8 | * with the License. You may obtain a copy of the License at 9 | * 10 | * http://www.apache.org/licenses/LICENSE-2.0 11 | * 12 | * Unless required by applicable law or agreed to in writing, software 13 | * distributed under the License is distributed on an "AS IS" BASIS, 14 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 15 | * See the License for the specific language governing permissions and 16 | * limitations under the License. 17 | */ 18 | package org.apache.twill.api; 19 | 20 | /** 21 | * Represents storage of secure tokens. 22 | */ 23 | public interface SecureStore { 24 | 25 | T getStore(); 26 | } 27 | -------------------------------------------------------------------------------- /twill-api/src/main/java/org/apache/twill/api/SecureStoreUpdater.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Licensed to the Apache Software Foundation (ASF) under one 3 | * or more contributor license agreements. See the NOTICE file 4 | * distributed with this work for additional information 5 | * regarding copyright ownership. The ASF licenses this file 6 | * to you under the Apache License, Version 2.0 (the 7 | * "License"); you may not use this file except in compliance 8 | * with the License. You may obtain a copy of the License at 9 | * 10 | * http://www.apache.org/licenses/LICENSE-2.0 11 | * 12 | * Unless required by applicable law or agreed to in writing, software 13 | * distributed under the License is distributed on an "AS IS" BASIS, 14 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 15 | * See the License for the specific language governing permissions and 16 | * limitations under the License. 17 | */ 18 | package org.apache.twill.api; 19 | 20 | import org.apache.twill.api.security.SecureStoreRenewer; 21 | 22 | /** 23 | * Represents class capable of creating update of {@link SecureStore} for live applications. 24 | * 25 | * @deprecated This class doesn't handle user correctly, use {@link SecureStoreRenewer} instead 26 | */ 27 | @Deprecated 28 | public interface SecureStoreUpdater { 29 | 30 | /** 31 | * Invoked when an update to SecureStore is needed. 32 | * 33 | * @param application The name of the application. 34 | * @param runId The runId of the live application. 35 | * @return A new {@link SecureStore}. 36 | */ 37 | SecureStore update(String application, RunId runId); 38 | } 39 | -------------------------------------------------------------------------------- /twill-api/src/main/java/org/apache/twill/api/ServiceAnnouncer.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Licensed to the Apache Software Foundation (ASF) under one 3 | * or more contributor license agreements. See the NOTICE file 4 | * distributed with this work for additional information 5 | * regarding copyright ownership. The ASF licenses this file 6 | * to you under the Apache License, Version 2.0 (the 7 | * "License"); you may not use this file except in compliance 8 | * with the License. You may obtain a copy of the License at 9 | * 10 | * http://www.apache.org/licenses/LICENSE-2.0 11 | * 12 | * Unless required by applicable law or agreed to in writing, software 13 | * distributed under the License is distributed on an "AS IS" BASIS, 14 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 15 | * See the License for the specific language governing permissions and 16 | * limitations under the License. 17 | */ 18 | package org.apache.twill.api; 19 | 20 | import org.apache.twill.common.Cancellable; 21 | 22 | /** 23 | * This interface provides a way to announce the availability of a service. 24 | */ 25 | public interface ServiceAnnouncer { 26 | 27 | /** 28 | * Registers an endpoint that could be discovered by external party. 29 | * @param serviceName Name of the endpoint 30 | * @param port Port of the endpoint 31 | */ 32 | Cancellable announce(String serviceName, int port); 33 | 34 | /** 35 | * Registers an endpoint that could be discovered by external party with a payload. 36 | * @param serviceName Name of the endpoint 37 | * @param port Port of the endpoint 38 | * @param payload byte array payload 39 | */ 40 | Cancellable announce(String serviceName, int port, byte[] payload); 41 | } 42 | -------------------------------------------------------------------------------- /twill-api/src/main/java/org/apache/twill/api/TwillApplication.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Licensed to the Apache Software Foundation (ASF) under one 3 | * or more contributor license agreements. See the NOTICE file 4 | * distributed with this work for additional information 5 | * regarding copyright ownership. The ASF licenses this file 6 | * to you under the Apache License, Version 2.0 (the 7 | * "License"); you may not use this file except in compliance 8 | * with the License. You may obtain a copy of the License at 9 | * 10 | * http://www.apache.org/licenses/LICENSE-2.0 11 | * 12 | * Unless required by applicable law or agreed to in writing, software 13 | * distributed under the License is distributed on an "AS IS" BASIS, 14 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 15 | * See the License for the specific language governing permissions and 16 | * limitations under the License. 17 | */ 18 | package org.apache.twill.api; 19 | 20 | /** 21 | * Represents a application that can be launched by Twill. 22 | */ 23 | public interface TwillApplication { 24 | 25 | /** 26 | * Invoked when launching the application on the client side. 27 | * @return A {@link TwillSpecification} specifying properties about this application. 28 | */ 29 | TwillSpecification configure(); 30 | } 31 | -------------------------------------------------------------------------------- /twill-api/src/main/java/org/apache/twill/api/TwillRunnerService.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Licensed to the Apache Software Foundation (ASF) under one 3 | * or more contributor license agreements. See the NOTICE file 4 | * distributed with this work for additional information 5 | * regarding copyright ownership. The ASF licenses this file 6 | * to you under the Apache License, Version 2.0 (the 7 | * "License"); you may not use this file except in compliance 8 | * with the License. You may obtain a copy of the License at 9 | * 10 | * http://www.apache.org/licenses/LICENSE-2.0 11 | * 12 | * Unless required by applicable law or agreed to in writing, software 13 | * distributed under the License is distributed on an "AS IS" BASIS, 14 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 15 | * See the License for the specific language governing permissions and 16 | * limitations under the License. 17 | */ 18 | package org.apache.twill.api; 19 | 20 | /** 21 | * A {@link TwillRunner} that provides lifecycle management functions. 22 | * The {@link #start()} method needs to be called before calling any other method of this interface. 23 | * When done with this service, call {@link #stop()} to release any resources that it holds. 24 | */ 25 | public interface TwillRunnerService extends TwillRunner { 26 | 27 | /** 28 | * Starts the service. Calling this method on a already started instance has no effect. 29 | * A service that is stopped cannot be started again. 30 | * 31 | * @throws RuntimeException if the service failed to start. 32 | */ 33 | void start(); 34 | 35 | /** 36 | * Stops the service. Calling this method on a already stopped instance has no effect. 37 | * 38 | * @throws RuntimeException if the service failed to start. 39 | */ 40 | void stop(); 41 | } 42 | -------------------------------------------------------------------------------- /twill-api/src/main/java/org/apache/twill/api/logging/LogHandler.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Licensed to the Apache Software Foundation (ASF) under one 3 | * or more contributor license agreements. See the NOTICE file 4 | * distributed with this work for additional information 5 | * regarding copyright ownership. The ASF licenses this file 6 | * to you under the Apache License, Version 2.0 (the 7 | * "License"); you may not use this file except in compliance 8 | * with the License. You may obtain a copy of the License at 9 | * 10 | * http://www.apache.org/licenses/LICENSE-2.0 11 | * 12 | * Unless required by applicable law or agreed to in writing, software 13 | * distributed under the License is distributed on an "AS IS" BASIS, 14 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 15 | * See the License for the specific language governing permissions and 16 | * limitations under the License. 17 | */ 18 | package org.apache.twill.api.logging; 19 | 20 | /** 21 | * Represents class that can receive logs from the application. 22 | */ 23 | public interface LogHandler { 24 | 25 | /** 26 | * Invokes when there is new {@link LogEntry} received from the application. 27 | */ 28 | void onLog(LogEntry logEntry); 29 | } 30 | -------------------------------------------------------------------------------- /twill-api/src/main/java/org/apache/twill/api/logging/LogThrowable.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Licensed to the Apache Software Foundation (ASF) under one 3 | * or more contributor license agreements. See the NOTICE file 4 | * distributed with this work for additional information 5 | * regarding copyright ownership. The ASF licenses this file 6 | * to you under the Apache License, Version 2.0 (the 7 | * "License"); you may not use this file except in compliance 8 | * with the License. You may obtain a copy of the License at 9 | * 10 | * http://www.apache.org/licenses/LICENSE-2.0 11 | * 12 | * Unless required by applicable law or agreed to in writing, software 13 | * distributed under the License is distributed on an "AS IS" BASIS, 14 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 15 | * See the License for the specific language governing permissions and 16 | * limitations under the License. 17 | */ 18 | package org.apache.twill.api.logging; 19 | 20 | /** 21 | * Carries {@link Throwable} information in a {@link LogEntry}. 22 | */ 23 | public interface LogThrowable { 24 | 25 | /** 26 | * Returns the name of the Throwable class. 27 | */ 28 | String getClassName(); 29 | 30 | /** 31 | * Returns the message contained inside the Throwable. 32 | * 33 | * @return A {@link String} message or {@code null} if such message is not available. 34 | */ 35 | String getMessage(); 36 | 37 | /** 38 | * Returns the stack trace of the Throwable. 39 | */ 40 | StackTraceElement[] getStackTraces(); 41 | 42 | /** 43 | * Returns the cause of this {@link LogThrowable}. 44 | * 45 | * @return The {@link LogThrowable} cause or {@code null} if no cause is available. 46 | */ 47 | LogThrowable getCause(); 48 | } 49 | -------------------------------------------------------------------------------- /twill-api/src/main/java/org/apache/twill/api/logging/package-info.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Licensed to the Apache Software Foundation (ASF) under one 3 | * or more contributor license agreements. See the NOTICE file 4 | * distributed with this work for additional information 5 | * regarding copyright ownership. The ASF licenses this file 6 | * to you under the Apache License, Version 2.0 (the 7 | * "License"); you may not use this file except in compliance 8 | * with the License. You may obtain a copy of the License at 9 | * 10 | * http://www.apache.org/licenses/LICENSE-2.0 11 | * 12 | * Unless required by applicable law or agreed to in writing, software 13 | * distributed under the License is distributed on an "AS IS" BASIS, 14 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 15 | * See the License for the specific language governing permissions and 16 | * limitations under the License. 17 | */ 18 | 19 | /** 20 | * This package contains class for handling logging events. 21 | */ 22 | package org.apache.twill.api.logging; 23 | -------------------------------------------------------------------------------- /twill-api/src/main/java/org/apache/twill/api/package-info.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Licensed to the Apache Software Foundation (ASF) under one 3 | * or more contributor license agreements. See the NOTICE file 4 | * distributed with this work for additional information 5 | * regarding copyright ownership. The ASF licenses this file 6 | * to you under the Apache License, Version 2.0 (the 7 | * "License"); you may not use this file except in compliance 8 | * with the License. You may obtain a copy of the License at 9 | * 10 | * http://www.apache.org/licenses/LICENSE-2.0 11 | * 12 | * Unless required by applicable law or agreed to in writing, software 13 | * distributed under the License is distributed on an "AS IS" BASIS, 14 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 15 | * See the License for the specific language governing permissions and 16 | * limitations under the License. 17 | */ 18 | /** 19 | * Classes in this package provides core functionality of the Twill library. 20 | */ 21 | package org.apache.twill.api; 22 | -------------------------------------------------------------------------------- /twill-api/src/main/java/org/apache/twill/api/security/SecureStoreRenewer.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Licensed to the Apache Software Foundation (ASF) under one 3 | * or more contributor license agreements. See the NOTICE file 4 | * distributed with this work for additional information 5 | * regarding copyright ownership. The ASF licenses this file 6 | * to you under the Apache License, Version 2.0 (the 7 | * "License"); you may not use this file except in compliance 8 | * with the License. You may obtain a copy of the License at 9 | * 10 | * http://www.apache.org/licenses/LICENSE-2.0 11 | * 12 | * Unless required by applicable law or agreed to in writing, software 13 | * distributed under the License is distributed on an "AS IS" BASIS, 14 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 15 | * See the License for the specific language governing permissions and 16 | * limitations under the License. 17 | */ 18 | package org.apache.twill.api.security; 19 | 20 | import org.apache.twill.api.RunId; 21 | import org.apache.twill.api.SecureStore; 22 | import org.apache.twill.filesystem.Location; 23 | 24 | import java.io.IOException; 25 | 26 | /** 27 | * This class is responsible for renewing the secure store used by application. 28 | */ 29 | public abstract class SecureStoreRenewer { 30 | 31 | /** 32 | * Renew the secure store for an application run. It must uses the {@link SecureStoreWriter} to update the 33 | * {@link SecureStore}. 34 | * 35 | * @param application The name of the application. 36 | * @param runId The runId of the live application. 37 | * @param secureStoreWriter a {@link SecureStoreWriter} for writing out the new {@link SecureStore}. 38 | * @throws IOException if renewal failed 39 | */ 40 | public abstract void renew(String application, RunId runId, SecureStoreWriter secureStoreWriter) throws IOException; 41 | } 42 | -------------------------------------------------------------------------------- /twill-api/src/main/java/org/apache/twill/api/security/SecureStoreWriter.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Licensed to the Apache Software Foundation (ASF) under one 3 | * or more contributor license agreements. See the NOTICE file 4 | * distributed with this work for additional information 5 | * regarding copyright ownership. The ASF licenses this file 6 | * to you under the Apache License, Version 2.0 (the 7 | * "License"); you may not use this file except in compliance 8 | * with the License. You may obtain a copy of the License at 9 | * 10 | * http://www.apache.org/licenses/LICENSE-2.0 11 | * 12 | * Unless required by applicable law or agreed to in writing, software 13 | * distributed under the License is distributed on an "AS IS" BASIS, 14 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 15 | * See the License for the specific language governing permissions and 16 | * limitations under the License. 17 | */ 18 | package org.apache.twill.api.security; 19 | 20 | import org.apache.twill.api.SecureStore; 21 | 22 | import java.io.IOException; 23 | 24 | /** 25 | * A writer provided to {@link SecureStoreRenewer} for writing out {@link SecureStore} during renewal process. 26 | */ 27 | public interface SecureStoreWriter { 28 | 29 | /** 30 | * Writes the given {@link SecureStore}. 31 | * 32 | * @param secureStore the {@link SecureStore} to persist 33 | * @throws IOException if failed to write out the {@link SecureStore} 34 | */ 35 | void write(SecureStore secureStore) throws IOException; 36 | } 37 | -------------------------------------------------------------------------------- /twill-api/src/main/java/org/apache/twill/internal/DefaultEventHandlerSpecification.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Licensed to the Apache Software Foundation (ASF) under one 3 | * or more contributor license agreements. See the NOTICE file 4 | * distributed with this work for additional information 5 | * regarding copyright ownership. The ASF licenses this file 6 | * to you under the Apache License, Version 2.0 (the 7 | * "License"); you may not use this file except in compliance 8 | * with the License. You may obtain a copy of the License at 9 | * 10 | * http://www.apache.org/licenses/LICENSE-2.0 11 | * 12 | * Unless required by applicable law or agreed to in writing, software 13 | * distributed under the License is distributed on an "AS IS" BASIS, 14 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 15 | * See the License for the specific language governing permissions and 16 | * limitations under the License. 17 | */ 18 | package org.apache.twill.internal; 19 | 20 | import org.apache.twill.api.EventHandler; 21 | import org.apache.twill.api.EventHandlerSpecification; 22 | 23 | import java.util.Collections; 24 | import java.util.HashMap; 25 | import java.util.Map; 26 | 27 | /** 28 | * 29 | */ 30 | public class DefaultEventHandlerSpecification implements EventHandlerSpecification { 31 | 32 | private final String className; 33 | private final Map configs; 34 | 35 | public DefaultEventHandlerSpecification(String className, Map configs) { 36 | this.className = className; 37 | this.configs = configs; 38 | } 39 | 40 | public DefaultEventHandlerSpecification(EventHandler eventHandler) { 41 | EventHandlerSpecification spec = eventHandler.configure(); 42 | this.className = eventHandler.getClass().getName(); 43 | this.configs = Collections.unmodifiableMap(new HashMap(spec.getConfigs())); 44 | } 45 | 46 | @Override 47 | public String getClassName() { 48 | return className; 49 | } 50 | 51 | @Override 52 | public Map getConfigs() { 53 | return configs; 54 | } 55 | } 56 | -------------------------------------------------------------------------------- /twill-api/src/main/java/org/apache/twill/internal/DefaultResourceSpecification.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Licensed to the Apache Software Foundation (ASF) under one 3 | * or more contributor license agreements. See the NOTICE file 4 | * distributed with this work for additional information 5 | * regarding copyright ownership. The ASF licenses this file 6 | * to you under the Apache License, Version 2.0 (the 7 | * "License"); you may not use this file except in compliance 8 | * with the License. You may obtain a copy of the License at 9 | * 10 | * http://www.apache.org/licenses/LICENSE-2.0 11 | * 12 | * Unless required by applicable law or agreed to in writing, software 13 | * distributed under the License is distributed on an "AS IS" BASIS, 14 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 15 | * See the License for the specific language governing permissions and 16 | * limitations under the License. 17 | */ 18 | package org.apache.twill.internal; 19 | 20 | import org.apache.twill.api.ResourceSpecification; 21 | 22 | /** 23 | * Straightforward implementation of {@link org.apache.twill.api.ResourceSpecification}. 24 | */ 25 | public final class DefaultResourceSpecification implements ResourceSpecification { 26 | private final int virtualCores; 27 | private final int memorySize; 28 | private final int instances; 29 | private final int uplink; 30 | private final int downlink; 31 | 32 | public DefaultResourceSpecification(int virtualCores, int memorySize, int instances, int uplink, int downlink) { 33 | this.virtualCores = virtualCores; 34 | this.memorySize = memorySize; 35 | this.instances = instances; 36 | this.uplink = uplink; 37 | this.downlink = downlink; 38 | } 39 | 40 | @Deprecated 41 | @Override 42 | public int getCores() { 43 | return virtualCores; 44 | } 45 | 46 | @Override 47 | public int getVirtualCores() { 48 | return virtualCores; 49 | } 50 | 51 | @Override 52 | public int getMemorySize() { 53 | return memorySize; 54 | } 55 | 56 | @Override 57 | public int getInstances() { 58 | return instances; 59 | } 60 | 61 | @Override 62 | public int getUplink() { 63 | return uplink; 64 | } 65 | 66 | @Override 67 | public int getDownlink() { 68 | return downlink; 69 | } 70 | } 71 | -------------------------------------------------------------------------------- /twill-api/src/main/java/org/apache/twill/internal/DefaultTwillRunnableSpecification.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Licensed to the Apache Software Foundation (ASF) under one 3 | * or more contributor license agreements. See the NOTICE file 4 | * distributed with this work for additional information 5 | * regarding copyright ownership. The ASF licenses this file 6 | * to you under the Apache License, Version 2.0 (the 7 | * "License"); you may not use this file except in compliance 8 | * with the License. You may obtain a copy of the License at 9 | * 10 | * http://www.apache.org/licenses/LICENSE-2.0 11 | * 12 | * Unless required by applicable law or agreed to in writing, software 13 | * distributed under the License is distributed on an "AS IS" BASIS, 14 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 15 | * See the License for the specific language governing permissions and 16 | * limitations under the License. 17 | */ 18 | package org.apache.twill.internal; 19 | 20 | import org.apache.twill.api.TwillRunnableSpecification; 21 | 22 | import java.util.Collections; 23 | import java.util.HashMap; 24 | import java.util.Map; 25 | 26 | /** 27 | * Straightforward implementation of {@link org.apache.twill.api.TwillRunnableSpecification}. 28 | */ 29 | public final class DefaultTwillRunnableSpecification implements TwillRunnableSpecification { 30 | 31 | private final String className; 32 | private final String name; 33 | private final Map arguments; 34 | 35 | public DefaultTwillRunnableSpecification(String className, String name, Map arguments) { 36 | this.className = className; 37 | this.name = name; 38 | this.arguments = Collections.unmodifiableMap(new HashMap(arguments)); 39 | } 40 | 41 | public DefaultTwillRunnableSpecification(String className, TwillRunnableSpecification other) { 42 | this.className = className; 43 | this.name = other.getName(); 44 | this.arguments = Collections.unmodifiableMap(new HashMap(other.getConfigs())); 45 | } 46 | 47 | @Override 48 | public String getClassName() { 49 | return className; 50 | } 51 | 52 | @Override 53 | public String getName() { 54 | return name; 55 | } 56 | 57 | @Override 58 | public Map getConfigs() { 59 | return arguments; 60 | } 61 | } 62 | -------------------------------------------------------------------------------- /twill-api/src/main/java/org/apache/twill/internal/RunIds.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Licensed to the Apache Software Foundation (ASF) under one 3 | * or more contributor license agreements. See the NOTICE file 4 | * distributed with this work for additional information 5 | * regarding copyright ownership. The ASF licenses this file 6 | * to you under the Apache License, Version 2.0 (the 7 | * "License"); you may not use this file except in compliance 8 | * with the License. You may obtain a copy of the License at 9 | * 10 | * http://www.apache.org/licenses/LICENSE-2.0 11 | * 12 | * Unless required by applicable law or agreed to in writing, software 13 | * distributed under the License is distributed on an "AS IS" BASIS, 14 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 15 | * See the License for the specific language governing permissions and 16 | * limitations under the License. 17 | */ 18 | package org.apache.twill.internal; 19 | 20 | import org.apache.twill.api.RunId; 21 | 22 | import java.util.UUID; 23 | 24 | /** 25 | * Factory class for creating instance of {@link org.apache.twill.api.RunId}. 26 | */ 27 | public final class RunIds { 28 | 29 | public static RunId generate() { 30 | return new RunIdImpl(UUID.randomUUID().toString()); 31 | } 32 | 33 | public static RunId fromString(String str) { 34 | return new RunIdImpl(str); 35 | } 36 | 37 | private RunIds() { 38 | } 39 | 40 | private static final class RunIdImpl implements RunId { 41 | 42 | final String id; 43 | 44 | private RunIdImpl(String id) { 45 | if (id == null) { 46 | throw new IllegalArgumentException("RunId cannot be null."); 47 | } 48 | this.id = id; 49 | } 50 | 51 | @Override 52 | public String getId() { 53 | return id; 54 | } 55 | 56 | @Override 57 | public String toString() { 58 | return getId(); 59 | } 60 | 61 | @Override 62 | public boolean equals(Object other) { 63 | if (this == other) { 64 | return true; 65 | } 66 | if (other == null || !(other instanceof RunId)) { 67 | return false; 68 | } 69 | return id.equals(((RunId) other).getId()); 70 | } 71 | 72 | @Override 73 | public int hashCode() { 74 | return id.hashCode(); 75 | } 76 | } 77 | } 78 | -------------------------------------------------------------------------------- /twill-api/src/main/java/org/apache/twill/internal/package-info.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Licensed to the Apache Software Foundation (ASF) under one 3 | * or more contributor license agreements. See the NOTICE file 4 | * distributed with this work for additional information 5 | * regarding copyright ownership. The ASF licenses this file 6 | * to you under the Apache License, Version 2.0 (the 7 | * "License"); you may not use this file except in compliance 8 | * with the License. You may obtain a copy of the License at 9 | * 10 | * http://www.apache.org/licenses/LICENSE-2.0 11 | * 12 | * Unless required by applicable law or agreed to in writing, software 13 | * distributed under the License is distributed on an "AS IS" BASIS, 14 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 15 | * See the License for the specific language governing permissions and 16 | * limitations under the License. 17 | */ 18 | 19 | /** 20 | * Internal classes for Twill API. 21 | */ 22 | package org.apache.twill.internal; 23 | -------------------------------------------------------------------------------- /twill-common/pom.xml: -------------------------------------------------------------------------------- 1 | 2 | 19 | 22 | 23 | twill-parent 24 | org.apache.twill 25 | 0.15.0-SNAPSHOT 26 | 27 | 4.0.0 28 | 29 | twill-common 30 | Apache Twill common library 31 | 32 | 33 | 34 | com.google.code.findbugs 35 | jsr305 36 | 37 | 38 | org.slf4j 39 | slf4j-api 40 | 41 | 42 | junit 43 | junit 44 | 45 | 46 | 47 | -------------------------------------------------------------------------------- /twill-common/src/main/java/org/apache/twill/common/Cancellable.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Licensed to the Apache Software Foundation (ASF) under one 3 | * or more contributor license agreements. See the NOTICE file 4 | * distributed with this work for additional information 5 | * regarding copyright ownership. The ASF licenses this file 6 | * to you under the Apache License, Version 2.0 (the 7 | * "License"); you may not use this file except in compliance 8 | * with the License. You may obtain a copy of the License at 9 | * 10 | * http://www.apache.org/licenses/LICENSE-2.0 11 | * 12 | * Unless required by applicable law or agreed to in writing, software 13 | * distributed under the License is distributed on an "AS IS" BASIS, 14 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 15 | * See the License for the specific language governing permissions and 16 | * limitations under the License. 17 | */ 18 | 19 | package org.apache.twill.common; 20 | 21 | /** 22 | * Something, usually a task, that can be cancelled. Cancellation is performed by the cancel method. 23 | */ 24 | public interface Cancellable { 25 | /** 26 | * Attempts to cancel execution of this task. 27 | */ 28 | void cancel(); 29 | } 30 | -------------------------------------------------------------------------------- /twill-common/src/main/java/org/apache/twill/common/Threads.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Licensed to the Apache Software Foundation (ASF) under one 3 | * or more contributor license agreements. See the NOTICE file 4 | * distributed with this work for additional information 5 | * regarding copyright ownership. The ASF licenses this file 6 | * to you under the Apache License, Version 2.0 (the 7 | * "License"); you may not use this file except in compliance 8 | * with the License. You may obtain a copy of the License at 9 | * 10 | * http://www.apache.org/licenses/LICENSE-2.0 11 | * 12 | * Unless required by applicable law or agreed to in writing, software 13 | * distributed under the License is distributed on an "AS IS" BASIS, 14 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 15 | * See the License for the specific language governing permissions and 16 | * limitations under the License. 17 | */ 18 | package org.apache.twill.common; 19 | 20 | import java.util.concurrent.Executor; 21 | import java.util.concurrent.ThreadFactory; 22 | import java.util.concurrent.atomic.AtomicLong; 23 | 24 | /** 25 | * 26 | */ 27 | public final class Threads { 28 | 29 | /** 30 | * A executor that execute task from the submitter thread. 31 | */ 32 | public static final Executor SAME_THREAD_EXECUTOR = new Executor() { 33 | @Override 34 | public void execute(Runnable command) { 35 | command.run(); 36 | } 37 | }; 38 | 39 | /** 40 | * Handy method to create {@link ThreadFactory} that creates daemon threads with the given name format. 41 | * 42 | * @param nameFormat Name format for the thread names. It should be a format string compatible 43 | * with the {@link String#format(String, Object...)} that takes a single number as the format 44 | * argument. 45 | * @return A {@link ThreadFactory}. 46 | */ 47 | public static ThreadFactory createDaemonThreadFactory(final String nameFormat) { 48 | final AtomicLong id = new AtomicLong(0); 49 | return new ThreadFactory() { 50 | @Override 51 | public Thread newThread(Runnable r) { 52 | Thread t = new Thread(r); 53 | t.setDaemon(true); 54 | t.setName(String.format(nameFormat, id.getAndIncrement())); 55 | return t; 56 | } 57 | }; 58 | } 59 | 60 | private Threads() { 61 | } 62 | } 63 | -------------------------------------------------------------------------------- /twill-common/src/main/java/org/apache/twill/filesystem/ForwardingLocationFactory.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Licensed to the Apache Software Foundation (ASF) under one 3 | * or more contributor license agreements. See the NOTICE file 4 | * distributed with this work for additional information 5 | * regarding copyright ownership. The ASF licenses this file 6 | * to you under the Apache License, Version 2.0 (the 7 | * "License"); you may not use this file except in compliance 8 | * with the License. You may obtain a copy of the License at 9 | * 10 | * http://www.apache.org/licenses/LICENSE-2.0 11 | * 12 | * Unless required by applicable law or agreed to in writing, software 13 | * distributed under the License is distributed on an "AS IS" BASIS, 14 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 15 | * See the License for the specific language governing permissions and 16 | * limitations under the License. 17 | */ 18 | package org.apache.twill.filesystem; 19 | 20 | /** 21 | * 22 | */ 23 | public abstract class ForwardingLocationFactory implements LocationFactory { 24 | 25 | private final LocationFactory delegate; 26 | 27 | protected ForwardingLocationFactory(LocationFactory delegate) { 28 | this.delegate = delegate; 29 | } 30 | 31 | public LocationFactory getDelegate() { 32 | return delegate; 33 | } 34 | } 35 | -------------------------------------------------------------------------------- /twill-common/src/main/java/org/apache/twill/filesystem/LocalLocationFactory.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Licensed to the Apache Software Foundation (ASF) under one 3 | * or more contributor license agreements. See the NOTICE file 4 | * distributed with this work for additional information 5 | * regarding copyright ownership. The ASF licenses this file 6 | * to you under the Apache License, Version 2.0 (the 7 | * "License"); you may not use this file except in compliance 8 | * with the License. You may obtain a copy of the License at 9 | * 10 | * http://www.apache.org/licenses/LICENSE-2.0 11 | * 12 | * Unless required by applicable law or agreed to in writing, software 13 | * distributed under the License is distributed on an "AS IS" BASIS, 14 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 15 | * See the License for the specific language governing permissions and 16 | * limitations under the License. 17 | */ 18 | package org.apache.twill.filesystem; 19 | 20 | import java.io.File; 21 | import java.net.URI; 22 | 23 | /** 24 | * A {@link LocationFactory} for creating local file {@link Location}. 25 | */ 26 | public final class LocalLocationFactory implements LocationFactory { 27 | 28 | private final File basePath; 29 | 30 | /** 31 | * Constructs a LocalLocationFactory that Location created will be relative to system root. 32 | */ 33 | public LocalLocationFactory() { 34 | this(new File(File.separator)); 35 | } 36 | 37 | public LocalLocationFactory(File basePath) { 38 | this.basePath = basePath; 39 | } 40 | 41 | @Override 42 | public Location create(String path) { 43 | return create(new File(basePath, path).toURI()); 44 | } 45 | 46 | @Override 47 | public Location create(URI uri) { 48 | if (uri.isAbsolute()) { 49 | return new LocalLocation(this, new File(uri)); 50 | } 51 | return new LocalLocation(this, new File(basePath, uri.getPath())); 52 | } 53 | 54 | @Override 55 | public Location getHomeLocation() { 56 | return new LocalLocation(this, new File(System.getProperty("user.home"))); 57 | } 58 | } 59 | -------------------------------------------------------------------------------- /twill-common/src/main/java/org/apache/twill/filesystem/LocationFactories.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Licensed to the Apache Software Foundation (ASF) under one 3 | * or more contributor license agreements. See the NOTICE file 4 | * distributed with this work for additional information 5 | * regarding copyright ownership. The ASF licenses this file 6 | * to you under the Apache License, Version 2.0 (the 7 | * "License"); you may not use this file except in compliance 8 | * with the License. You may obtain a copy of the License at 9 | * 10 | * http://www.apache.org/licenses/LICENSE-2.0 11 | * 12 | * Unless required by applicable law or agreed to in writing, software 13 | * distributed under the License is distributed on an "AS IS" BASIS, 14 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 15 | * See the License for the specific language governing permissions and 16 | * limitations under the License. 17 | */ 18 | package org.apache.twill.filesystem; 19 | 20 | import java.io.IOException; 21 | import java.net.URI; 22 | 23 | /** 24 | * Provides helper methods for creating different {@link LocationFactory}. 25 | */ 26 | public final class LocationFactories { 27 | 28 | /** 29 | * Creates a {@link LocationFactory} that always applies the giving namespace prefix. 30 | */ 31 | public static LocationFactory namespace(LocationFactory delegate, final String namespace) { 32 | return new ForwardingLocationFactory(delegate) { 33 | @Override 34 | public Location create(String path) { 35 | try { 36 | Location base = getDelegate().create(namespace); 37 | return base.append(path); 38 | } catch (IOException e) { 39 | throw new RuntimeException(e); 40 | } 41 | } 42 | 43 | @Override 44 | public Location create(URI uri) { 45 | if (uri.isAbsolute()) { 46 | return getDelegate().create(uri); 47 | } 48 | try { 49 | Location base = getDelegate().create(namespace); 50 | return base.append(uri.getPath()); 51 | } catch (IOException e) { 52 | throw new RuntimeException(e); 53 | } 54 | } 55 | 56 | @Override 57 | public Location getHomeLocation() { 58 | return getDelegate().getHomeLocation(); 59 | } 60 | }; 61 | } 62 | 63 | private LocationFactories() { 64 | } 65 | } 66 | -------------------------------------------------------------------------------- /twill-common/src/main/java/org/apache/twill/filesystem/LocationFactory.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Licensed to the Apache Software Foundation (ASF) under one 3 | * or more contributor license agreements. See the NOTICE file 4 | * distributed with this work for additional information 5 | * regarding copyright ownership. The ASF licenses this file 6 | * to you under the Apache License, Version 2.0 (the 7 | * "License"); you may not use this file except in compliance 8 | * with the License. You may obtain a copy of the License at 9 | * 10 | * http://www.apache.org/licenses/LICENSE-2.0 11 | * 12 | * Unless required by applicable law or agreed to in writing, software 13 | * distributed under the License is distributed on an "AS IS" BASIS, 14 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 15 | * See the License for the specific language governing permissions and 16 | * limitations under the License. 17 | */ 18 | package org.apache.twill.filesystem; 19 | 20 | import java.net.URI; 21 | 22 | /** 23 | * Factory for creating instance of {@link Location}. 24 | */ 25 | public interface LocationFactory { 26 | 27 | /** 28 | * Creates an instance of {@link Location} of the given path. 29 | * @param path The path representing the location. 30 | * @return An instance of {@link Location}. 31 | */ 32 | Location create(String path); 33 | 34 | /** 35 | * Creates an instance of {@link Location} based on {@link java.net.URI} uri. 36 | * 37 | * @param uri to the resource on the filesystem. 38 | * @return An instance of {@link Location} 39 | */ 40 | Location create(URI uri); 41 | 42 | /** 43 | * Returns the home location. 44 | */ 45 | Location getHomeLocation(); 46 | } 47 | -------------------------------------------------------------------------------- /twill-core/src/main/java/org/apache/twill/internal/Arguments.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Licensed to the Apache Software Foundation (ASF) under one 3 | * or more contributor license agreements. See the NOTICE file 4 | * distributed with this work for additional information 5 | * regarding copyright ownership. The ASF licenses this file 6 | * to you under the Apache License, Version 2.0 (the 7 | * "License"); you may not use this file except in compliance 8 | * with the License. You may obtain a copy of the License at 9 | * 10 | * http://www.apache.org/licenses/LICENSE-2.0 11 | * 12 | * Unless required by applicable law or agreed to in writing, software 13 | * distributed under the License is distributed on an "AS IS" BASIS, 14 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 15 | * See the License for the specific language governing permissions and 16 | * limitations under the License. 17 | */ 18 | package org.apache.twill.internal; 19 | 20 | import com.google.common.collect.ImmutableList; 21 | import com.google.common.collect.ImmutableMultimap; 22 | import com.google.common.collect.Multimap; 23 | 24 | import java.util.List; 25 | 26 | /** 27 | * Class that encapsulate application arguments and per runnable arguments. 28 | */ 29 | public final class Arguments { 30 | 31 | private final List arguments; 32 | private final Multimap runnableArguments; 33 | 34 | public Arguments(List arguments, Multimap runnableArguments) { 35 | this.arguments = ImmutableList.copyOf(arguments); 36 | this.runnableArguments = ImmutableMultimap.copyOf(runnableArguments); 37 | } 38 | 39 | public List getArguments() { 40 | return arguments; 41 | } 42 | 43 | public Multimap getRunnableArguments() { 44 | return runnableArguments; 45 | } 46 | } 47 | -------------------------------------------------------------------------------- /twill-core/src/main/java/org/apache/twill/internal/ContainerInfo.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Licensed to the Apache Software Foundation (ASF) under one 3 | * or more contributor license agreements. See the NOTICE file 4 | * distributed with this work for additional information 5 | * regarding copyright ownership. The ASF licenses this file 6 | * to you under the Apache License, Version 2.0 (the 7 | * "License"); you may not use this file except in compliance 8 | * with the License. You may obtain a copy of the License at 9 | * 10 | * http://www.apache.org/licenses/LICENSE-2.0 11 | * 12 | * Unless required by applicable law or agreed to in writing, software 13 | * distributed under the License is distributed on an "AS IS" BASIS, 14 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 15 | * See the License for the specific language governing permissions and 16 | * limitations under the License. 17 | */ 18 | package org.apache.twill.internal; 19 | 20 | import java.net.InetAddress; 21 | 22 | /** 23 | * Represents information of the container that the processing is/will be running in. 24 | */ 25 | public interface ContainerInfo extends ResourceCapability { 26 | 27 | /** 28 | * Returns the ID of the container. 29 | */ 30 | String getId(); 31 | 32 | /** 33 | * Returns the host information of the container. 34 | */ 35 | InetAddress getHost(); 36 | 37 | /** 38 | * Returns the port for communicating to the container host. 39 | */ 40 | int getPort(); 41 | } 42 | -------------------------------------------------------------------------------- /twill-core/src/main/java/org/apache/twill/internal/ContainerLiveNodeData.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Licensed to the Apache Software Foundation (ASF) under one 3 | * or more contributor license agreements. See the NOTICE file 4 | * distributed with this work for additional information 5 | * regarding copyright ownership. The ASF licenses this file 6 | * to you under the Apache License, Version 2.0 (the 7 | * "License"); you may not use this file except in compliance 8 | * with the License. You may obtain a copy of the License at 9 | * 10 | * http://www.apache.org/licenses/LICENSE-2.0 11 | * 12 | * Unless required by applicable law or agreed to in writing, software 13 | * distributed under the License is distributed on an "AS IS" BASIS, 14 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 15 | * See the License for the specific language governing permissions and 16 | * limitations under the License. 17 | */ 18 | package org.apache.twill.internal; 19 | 20 | import java.util.HashMap; 21 | import java.util.Map; 22 | import javax.annotation.Nullable; 23 | 24 | /** 25 | * 26 | */ 27 | public final class ContainerLiveNodeData { 28 | private final String containerId; 29 | private final String host; 30 | private final String debugPort; 31 | private final Map logLevels; 32 | 33 | public ContainerLiveNodeData(String containerId, String host, String debugPort, 34 | Map logLevels) { 35 | this.containerId = containerId; 36 | this.host = host; 37 | this.debugPort = debugPort; 38 | this.logLevels = new HashMap<>(logLevels); 39 | } 40 | 41 | public String getContainerId() { 42 | return containerId; 43 | } 44 | 45 | public String getHost() { 46 | return host; 47 | } 48 | 49 | public String getDebugPort() { 50 | return debugPort; 51 | } 52 | 53 | public Map getLogLevels() { 54 | return logLevels; 55 | } 56 | 57 | public void setLogLevel(String loggerName, @Nullable String logLevel) { 58 | logLevels.put(loggerName, logLevel); 59 | } 60 | 61 | public void removeLogLevel(String loggerName) { 62 | logLevels.remove(loggerName); 63 | } 64 | } 65 | -------------------------------------------------------------------------------- /twill-core/src/main/java/org/apache/twill/internal/EnvContainerInfo.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Licensed to the Apache Software Foundation (ASF) under one 3 | * or more contributor license agreements. See the NOTICE file 4 | * distributed with this work for additional information 5 | * regarding copyright ownership. The ASF licenses this file 6 | * to you under the Apache License, Version 2.0 (the 7 | * "License"); you may not use this file except in compliance 8 | * with the License. You may obtain a copy of the License at 9 | * 10 | * http://www.apache.org/licenses/LICENSE-2.0 11 | * 12 | * Unless required by applicable law or agreed to in writing, software 13 | * distributed under the License is distributed on an "AS IS" BASIS, 14 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 15 | * See the License for the specific language governing permissions and 16 | * limitations under the License. 17 | */ 18 | package org.apache.twill.internal; 19 | 20 | import java.net.InetAddress; 21 | import java.net.UnknownHostException; 22 | 23 | /** 24 | * A {@link ContainerInfo} based on information on the environment. 25 | */ 26 | public final class EnvContainerInfo implements ContainerInfo { 27 | private final String id; 28 | private final InetAddress host; 29 | private final int port; 30 | private final int virtualCores; 31 | private final int memoryMB; 32 | 33 | public EnvContainerInfo() throws UnknownHostException { 34 | id = System.getenv(EnvKeys.YARN_CONTAINER_ID); 35 | host = InetAddress.getByName(System.getenv(EnvKeys.YARN_CONTAINER_HOST)); 36 | port = Integer.parseInt(System.getenv(EnvKeys.YARN_CONTAINER_PORT)); 37 | virtualCores = Integer.parseInt(System.getenv(EnvKeys.YARN_CONTAINER_VIRTUAL_CORES)); 38 | memoryMB = Integer.parseInt(System.getenv(EnvKeys.YARN_CONTAINER_MEMORY_MB)); 39 | } 40 | 41 | @Override 42 | public String getId() { 43 | return id; 44 | } 45 | 46 | @Override 47 | public InetAddress getHost() { 48 | return host; 49 | } 50 | 51 | @Override 52 | public int getPort() { 53 | return port; 54 | } 55 | 56 | @Override 57 | public int getMemoryMB() { 58 | return memoryMB; 59 | } 60 | 61 | @Override 62 | public int getVirtualCores() { 63 | return virtualCores; 64 | } 65 | } 66 | -------------------------------------------------------------------------------- /twill-core/src/main/java/org/apache/twill/internal/EnvKeys.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Licensed to the Apache Software Foundation (ASF) under one 3 | * or more contributor license agreements. See the NOTICE file 4 | * distributed with this work for additional information 5 | * regarding copyright ownership. The ASF licenses this file 6 | * to you under the Apache License, Version 2.0 (the 7 | * "License"); you may not use this file except in compliance 8 | * with the License. You may obtain a copy of the License at 9 | * 10 | * http://www.apache.org/licenses/LICENSE-2.0 11 | * 12 | * Unless required by applicable law or agreed to in writing, software 13 | * distributed under the License is distributed on an "AS IS" BASIS, 14 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 15 | * See the License for the specific language governing permissions and 16 | * limitations under the License. 17 | */ 18 | package org.apache.twill.internal; 19 | 20 | /** 21 | * Places for define common environment keys. 22 | */ 23 | public final class EnvKeys { 24 | 25 | public static final String TWILL_RUN_ID = "TWILL_RUN_ID"; 26 | public static final String TWILL_INSTANCE_ID = "TWILL_INSTANCE_ID"; 27 | public static final String TWILL_INSTANCE_COUNT = "TWILL_INSTANCE_COUNT"; 28 | 29 | /** 30 | * Cluster filesystem directory for storing twill app related files. 31 | */ 32 | public static final String TWILL_RUNNABLE_NAME = "TWILL_RUNNABLE_NAME"; 33 | 34 | public static final String YARN_APP_ID = "YARN_APP_ID"; 35 | public static final String YARN_APP_ID_CLUSTER_TIME = "YARN_APP_ID_CLUSTER_TIME"; 36 | public static final String YARN_APP_ID_STR = "YARN_APP_ID_STR"; 37 | 38 | public static final String YARN_CONTAINER_ID = "YARN_CONTAINER_ID"; 39 | public static final String YARN_CONTAINER_HOST = "YARN_CONTAINER_HOST"; 40 | public static final String YARN_CONTAINER_PORT = "YARN_CONTAINER_PORT"; 41 | 42 | /** 43 | * Used to inform runnables of their resource usage. 44 | */ 45 | public static final String YARN_CONTAINER_VIRTUAL_CORES = "YARN_CONTAINER_VIRTUAL_CORES"; 46 | public static final String YARN_CONTAINER_MEMORY_MB = "YARN_CONTAINER_MEMORY_MB"; 47 | 48 | private EnvKeys() { 49 | } 50 | } 51 | -------------------------------------------------------------------------------- /twill-core/src/main/java/org/apache/twill/internal/LogOnlyEventHandler.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Licensed to the Apache Software Foundation (ASF) under one 3 | * or more contributor license agreements. See the NOTICE file 4 | * distributed with this work for additional information 5 | * regarding copyright ownership. The ASF licenses this file 6 | * to you under the Apache License, Version 2.0 (the 7 | * "License"); you may not use this file except in compliance 8 | * with the License. You may obtain a copy of the License at 9 | * 10 | * http://www.apache.org/licenses/LICENSE-2.0 11 | * 12 | * Unless required by applicable law or agreed to in writing, software 13 | * distributed under the License is distributed on an "AS IS" BASIS, 14 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 15 | * See the License for the specific language governing permissions and 16 | * limitations under the License. 17 | */ 18 | package org.apache.twill.internal; 19 | 20 | import org.apache.twill.api.EventHandler; 21 | import org.slf4j.Logger; 22 | import org.slf4j.LoggerFactory; 23 | 24 | import java.util.concurrent.TimeUnit; 25 | 26 | /** 27 | * 28 | */ 29 | public final class LogOnlyEventHandler extends EventHandler { 30 | 31 | private static final Logger LOG = LoggerFactory.getLogger(LogOnlyEventHandler.class); 32 | 33 | @Override 34 | public TimeoutAction launchTimeout(Iterable timeoutEvents) { 35 | for (TimeoutEvent event : timeoutEvents) { 36 | LOG.info("Requested {} containers for runnable {}, only got {} after {} ms.", 37 | event.getExpectedInstances(), event.getRunnableName(), 38 | event.getActualInstances(), System.currentTimeMillis() - event.getRequestTime()); 39 | } 40 | return TimeoutAction.recheck(Constants.PROVISION_TIMEOUT, TimeUnit.MILLISECONDS); 41 | 42 | } 43 | } 44 | -------------------------------------------------------------------------------- /twill-core/src/main/java/org/apache/twill/internal/ProcessController.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Licensed to the Apache Software Foundation (ASF) under one 3 | * or more contributor license agreements. See the NOTICE file 4 | * distributed with this work for additional information 5 | * regarding copyright ownership. The ASF licenses this file 6 | * to you under the Apache License, Version 2.0 (the 7 | * "License"); you may not use this file except in compliance 8 | * with the License. You may obtain a copy of the License at 9 | * 10 | * http://www.apache.org/licenses/LICENSE-2.0 11 | * 12 | * Unless required by applicable law or agreed to in writing, software 13 | * distributed under the License is distributed on an "AS IS" BASIS, 14 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 15 | * See the License for the specific language governing permissions and 16 | * limitations under the License. 17 | */ 18 | package org.apache.twill.internal; 19 | 20 | import org.apache.twill.common.Cancellable; 21 | 22 | /** 23 | * For controlling a launch yarn process. 24 | * 25 | * @param Report type. 26 | */ 27 | public interface ProcessController extends AutoCloseable, Cancellable { 28 | 29 | R getReport(); 30 | 31 | /** 32 | * Request to stop the running process. 33 | */ 34 | void cancel(); 35 | } 36 | -------------------------------------------------------------------------------- /twill-core/src/main/java/org/apache/twill/internal/ResourceCapability.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Licensed to the Apache Software Foundation (ASF) under one 3 | * or more contributor license agreements. See the NOTICE file 4 | * distributed with this work for additional information 5 | * regarding copyright ownership. The ASF licenses this file 6 | * to you under the Apache License, Version 2.0 (the 7 | * "License"); you may not use this file except in compliance 8 | * with the License. You may obtain a copy of the License at 9 | * 10 | * http://www.apache.org/licenses/LICENSE-2.0 11 | * 12 | * Unless required by applicable law or agreed to in writing, software 13 | * distributed under the License is distributed on an "AS IS" BASIS, 14 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 15 | * See the License for the specific language governing permissions and 16 | * limitations under the License. 17 | */ 18 | package org.apache.twill.internal; 19 | 20 | /** 21 | * Represents information about compute resources capability. 22 | */ 23 | public interface ResourceCapability { 24 | 25 | /** 26 | * Returns memory size in MB. 27 | */ 28 | int getMemoryMB(); 29 | 30 | /** 31 | * Returns the number of virtual cpu cores. 32 | */ 33 | int getVirtualCores(); 34 | } 35 | -------------------------------------------------------------------------------- /twill-core/src/main/java/org/apache/twill/internal/ServiceListenerAdapter.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Licensed to the Apache Software Foundation (ASF) under one 3 | * or more contributor license agreements. See the NOTICE file 4 | * distributed with this work for additional information 5 | * regarding copyright ownership. The ASF licenses this file 6 | * to you under the Apache License, Version 2.0 (the 7 | * "License"); you may not use this file except in compliance 8 | * with the License. You may obtain a copy of the License at 9 | * 10 | * http://www.apache.org/licenses/LICENSE-2.0 11 | * 12 | * Unless required by applicable law or agreed to in writing, software 13 | * distributed under the License is distributed on an "AS IS" BASIS, 14 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 15 | * See the License for the specific language governing permissions and 16 | * limitations under the License. 17 | */ 18 | package org.apache.twill.internal; 19 | 20 | import com.google.common.util.concurrent.Service; 21 | 22 | /** 23 | * An adapter for implementing {@link Service.Listener} with all method default to no-op. 24 | */ 25 | public abstract class ServiceListenerAdapter implements Service.Listener { 26 | @Override 27 | public void starting() { 28 | // No-op 29 | } 30 | 31 | @Override 32 | public void running() { 33 | // No-op 34 | } 35 | 36 | @Override 37 | public void stopping(Service.State from) { 38 | // No-op 39 | } 40 | 41 | @Override 42 | public void terminated(Service.State from) { 43 | // No-op 44 | } 45 | 46 | @Override 47 | public void failed(Service.State from, Throwable failure) { 48 | // No-op 49 | } 50 | } 51 | -------------------------------------------------------------------------------- /twill-core/src/main/java/org/apache/twill/internal/SingleRunnableApplication.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Licensed to the Apache Software Foundation (ASF) under one 3 | * or more contributor license agreements. See the NOTICE file 4 | * distributed with this work for additional information 5 | * regarding copyright ownership. The ASF licenses this file 6 | * to you under the Apache License, Version 2.0 (the 7 | * "License"); you may not use this file except in compliance 8 | * with the License. You may obtain a copy of the License at 9 | * 10 | * http://www.apache.org/licenses/LICENSE-2.0 11 | * 12 | * Unless required by applicable law or agreed to in writing, software 13 | * distributed under the License is distributed on an "AS IS" BASIS, 14 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 15 | * See the License for the specific language governing permissions and 16 | * limitations under the License. 17 | */ 18 | package org.apache.twill.internal; 19 | 20 | import org.apache.twill.api.ResourceSpecification; 21 | import org.apache.twill.api.TwillApplication; 22 | import org.apache.twill.api.TwillRunnable; 23 | import org.apache.twill.api.TwillRunnableSpecification; 24 | import org.apache.twill.api.TwillSpecification; 25 | 26 | /** 27 | * A simple {@link TwillApplication} that contains only one {@link TwillRunnable}. 28 | */ 29 | public class SingleRunnableApplication implements TwillApplication { 30 | 31 | private final TwillRunnable runnable; 32 | private final ResourceSpecification resourceSpec; 33 | 34 | public SingleRunnableApplication(TwillRunnable runnable, ResourceSpecification resourceSpec) { 35 | this.runnable = runnable; 36 | this.resourceSpec = resourceSpec; 37 | } 38 | 39 | @Override 40 | public TwillSpecification configure() { 41 | TwillRunnableSpecification runnableSpec = runnable.configure(); 42 | return TwillSpecification.Builder.with() 43 | .setName(runnableSpec.getName()) 44 | .withRunnable().add(runnableSpec.getName(), runnable, resourceSpec) 45 | .noLocalFiles() 46 | .anyOrder() 47 | .build(); 48 | } 49 | } 50 | -------------------------------------------------------------------------------- /twill-core/src/main/java/org/apache/twill/internal/TwillContainerController.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Licensed to the Apache Software Foundation (ASF) under one 3 | * or more contributor license agreements. See the NOTICE file 4 | * distributed with this work for additional information 5 | * regarding copyright ownership. The ASF licenses this file 6 | * to you under the Apache License, Version 2.0 (the 7 | * "License"); you may not use this file except in compliance 8 | * with the License. You may obtain a copy of the License at 9 | * 10 | * http://www.apache.org/licenses/LICENSE-2.0 11 | * 12 | * Unless required by applicable law or agreed to in writing, software 13 | * distributed under the License is distributed on an "AS IS" BASIS, 14 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 15 | * See the License for the specific language governing permissions and 16 | * limitations under the License. 17 | */ 18 | package org.apache.twill.internal; 19 | 20 | import com.google.common.util.concurrent.ListenableFuture; 21 | import com.google.common.util.concurrent.Service; 22 | import org.apache.twill.api.ServiceController; 23 | import org.apache.twill.internal.state.Message; 24 | 25 | import javax.annotation.Nullable; 26 | 27 | /** 28 | * A {@link ServiceController} that allows sending a message directly. Internal use only. 29 | */ 30 | public interface TwillContainerController extends ServiceController, Service { 31 | 32 | ListenableFuture sendMessage(Message message); 33 | 34 | /** 35 | * Calls to indicated that the container that this controller is associated with is completed. 36 | * Any resources it hold will be releases and all pending futures will be cancelled. 37 | */ 38 | void completed(int exitStatus); 39 | 40 | /** 41 | * @return the container's live node data. 42 | */ 43 | @Nullable 44 | ContainerLiveNodeData getLiveNodeData(); 45 | 46 | /** 47 | * @return the instance ID of the runnable that running in the container controlled by this controller. 48 | */ 49 | int getInstanceId(); 50 | } 51 | -------------------------------------------------------------------------------- /twill-core/src/main/java/org/apache/twill/internal/io/BasicLocationCache.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Licensed to the Apache Software Foundation (ASF) under one 3 | * or more contributor license agreements. See the NOTICE file 4 | * distributed with this work for additional information 5 | * regarding copyright ownership. The ASF licenses this file 6 | * to you under the Apache License, Version 2.0 (the 7 | * "License"); you may not use this file except in compliance 8 | * with the License. You may obtain a copy of the License at 9 | * 10 | * http://www.apache.org/licenses/LICENSE-2.0 11 | * 12 | * Unless required by applicable law or agreed to in writing, software 13 | * distributed under the License is distributed on an "AS IS" BASIS, 14 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 15 | * See the License for the specific language governing permissions and 16 | * limitations under the License. 17 | */ 18 | package org.apache.twill.internal.io; 19 | 20 | import org.apache.twill.filesystem.Location; 21 | import org.slf4j.Logger; 22 | import org.slf4j.LoggerFactory; 23 | 24 | import java.io.IOException; 25 | 26 | /** 27 | * A straightforward implementation of {@link LocationCache} that simply use location existence as the cache 28 | * indicator. 29 | */ 30 | public class BasicLocationCache implements LocationCache { 31 | 32 | private static final Logger LOG = LoggerFactory.getLogger(BasicLocationCache.class); 33 | 34 | private final Location cacheDir; 35 | 36 | public BasicLocationCache(Location cacheDir) { 37 | this.cacheDir = cacheDir; 38 | } 39 | 40 | @Override 41 | public synchronized Location get(String name, Loader loader) throws IOException { 42 | Location location = cacheDir.append(name); 43 | if (location.exists()) { 44 | LOG.debug("Cache hit for {} in {}", name, location); 45 | return location; 46 | } 47 | 48 | LOG.debug("Cache miss for {}. Use Loader to save to {}", name, location); 49 | loader.load(name, location); 50 | return location; 51 | } 52 | } 53 | -------------------------------------------------------------------------------- /twill-core/src/main/java/org/apache/twill/internal/io/LocationCache.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Licensed to the Apache Software Foundation (ASF) under one 3 | * or more contributor license agreements. See the NOTICE file 4 | * distributed with this work for additional information 5 | * regarding copyright ownership. The ASF licenses this file 6 | * to you under the Apache License, Version 2.0 (the 7 | * "License"); you may not use this file except in compliance 8 | * with the License. You may obtain a copy of the License at 9 | * 10 | * http://www.apache.org/licenses/LICENSE-2.0 11 | * 12 | * Unless required by applicable law or agreed to in writing, software 13 | * distributed under the License is distributed on an "AS IS" BASIS, 14 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 15 | * See the License for the specific language governing permissions and 16 | * limitations under the License. 17 | */ 18 | package org.apache.twill.internal.io; 19 | 20 | import org.apache.twill.filesystem.Location; 21 | 22 | import java.io.IOException; 23 | 24 | /** 25 | * Defines caching of {@link Location}. 26 | */ 27 | public interface LocationCache { 28 | 29 | /** 30 | * Gets the {@link Location} represented by the given name. If there is no such name exists in the cache, 31 | * use the given {@link Loader} to populate the cache. 32 | * 33 | * @param name name of the caching entry 34 | * @param loader the {@link Loader} for populating the content for the cache entry 35 | * @return A {@link Location} associated with the cache name 36 | * @throws IOException If failed to load the {@link Location}. 37 | */ 38 | Location get(String name, Loader loader) throws IOException; 39 | 40 | /** 41 | * A loader to load the content of the given name. 42 | */ 43 | abstract class Loader { 44 | 45 | /** 46 | * Loads content into the given target {@link Location}. 47 | * 48 | * @param name name of the cache entry 49 | * @param targetLocation the target {@link Location} to store the content. 50 | * @throws IOException If failed to populate the content. 51 | */ 52 | public abstract void load(String name, Location targetLocation) throws IOException; 53 | } 54 | } 55 | -------------------------------------------------------------------------------- /twill-core/src/main/java/org/apache/twill/internal/io/NoCachingLocationCache.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Licensed to the Apache Software Foundation (ASF) under one 3 | * or more contributor license agreements. See the NOTICE file 4 | * distributed with this work for additional information 5 | * regarding copyright ownership. The ASF licenses this file 6 | * to you under the Apache License, Version 2.0 (the 7 | * "License"); you may not use this file except in compliance 8 | * with the License. You may obtain a copy of the License at 9 | * 10 | * http://www.apache.org/licenses/LICENSE-2.0 11 | * 12 | * Unless required by applicable law or agreed to in writing, software 13 | * distributed under the License is distributed on an "AS IS" BASIS, 14 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 15 | * See the License for the specific language governing permissions and 16 | * limitations under the License. 17 | */ 18 | package org.apache.twill.internal.io; 19 | 20 | import org.apache.twill.filesystem.Location; 21 | import org.apache.twill.internal.utils.Paths; 22 | 23 | import java.io.IOException; 24 | 25 | /** 26 | * A implementation of {@link LocationCache} that never cache any content. 27 | * It always invokes {@link Loader#load(String, Location)} to load the content. 28 | */ 29 | public class NoCachingLocationCache implements LocationCache { 30 | 31 | private final Location baseDir; 32 | 33 | public NoCachingLocationCache(Location baseDir) { 34 | this.baseDir = baseDir; 35 | } 36 | 37 | @Override 38 | public Location get(String name, Loader loader) throws IOException { 39 | String suffix = Paths.getExtension(name); 40 | String prefix = name.substring(0, name.length() - suffix.length() - 1); 41 | Location targetLocation = baseDir.append(prefix).getTempFile('.' + suffix); 42 | loader.load(name, targetLocation); 43 | return targetLocation; 44 | } 45 | } 46 | -------------------------------------------------------------------------------- /twill-core/src/main/java/org/apache/twill/internal/json/JsonUtils.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Licensed to the Apache Software Foundation (ASF) under one 3 | * or more contributor license agreements. See the NOTICE file 4 | * distributed with this work for additional information 5 | * regarding copyright ownership. The ASF licenses this file 6 | * to you under the Apache License, Version 2.0 (the 7 | * "License"); you may not use this file except in compliance 8 | * with the License. You may obtain a copy of the License at 9 | * 10 | * http://www.apache.org/licenses/LICENSE-2.0 11 | * 12 | * Unless required by applicable law or agreed to in writing, software 13 | * distributed under the License is distributed on an "AS IS" BASIS, 14 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 15 | * See the License for the specific language governing permissions and 16 | * limitations under the License. 17 | */ 18 | package org.apache.twill.internal.json; 19 | 20 | import com.google.gson.JsonElement; 21 | import com.google.gson.JsonObject; 22 | 23 | /** 24 | * Collections of helper functions for json codec. 25 | */ 26 | public final class JsonUtils { 27 | 28 | private JsonUtils() { 29 | } 30 | 31 | /** 32 | * Returns a String representation of the given property. 33 | */ 34 | public static String getAsString(JsonObject json, String property) { 35 | JsonElement jsonElement = json.get(property); 36 | if (jsonElement == null || jsonElement.isJsonNull()) { 37 | return null; 38 | } 39 | if (jsonElement.isJsonPrimitive()) { 40 | return jsonElement.getAsString(); 41 | } 42 | return jsonElement.toString(); 43 | } 44 | 45 | /** 46 | * Returns a long representation of the given property. 47 | */ 48 | public static long getAsLong(JsonObject json, String property, long defaultValue) { 49 | try { 50 | return json.get(property).getAsLong(); 51 | } catch (Exception e) { 52 | return defaultValue; 53 | } 54 | } 55 | 56 | /** 57 | * Returns a long representation of the given property. 58 | */ 59 | public static int getAsInt(JsonObject json, String property, int defaultValue) { 60 | try { 61 | return json.get(property).getAsInt(); 62 | } catch (Exception e) { 63 | return defaultValue; 64 | } 65 | } 66 | } 67 | -------------------------------------------------------------------------------- /twill-core/src/main/java/org/apache/twill/internal/kafka/client/BasicFetchedMessage.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Licensed to the Apache Software Foundation (ASF) under one 3 | * or more contributor license agreements. See the NOTICE file 4 | * distributed with this work for additional information 5 | * regarding copyright ownership. The ASF licenses this file 6 | * to you under the Apache License, Version 2.0 (the 7 | * "License"); you may not use this file except in compliance 8 | * with the License. You may obtain a copy of the License at 9 | * 10 | * http://www.apache.org/licenses/LICENSE-2.0 11 | * 12 | * Unless required by applicable law or agreed to in writing, software 13 | * distributed under the License is distributed on an "AS IS" BASIS, 14 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 15 | * See the License for the specific language governing permissions and 16 | * limitations under the License. 17 | */ 18 | package org.apache.twill.internal.kafka.client; 19 | 20 | import org.apache.twill.kafka.client.FetchedMessage; 21 | import org.apache.twill.kafka.client.TopicPartition; 22 | 23 | import java.nio.ByteBuffer; 24 | 25 | /** 26 | * An implementation of FetchedMessage that provides setters as well. 27 | */ 28 | final class BasicFetchedMessage implements FetchedMessage { 29 | 30 | private final TopicPartition topicPartition; 31 | private ByteBuffer payload; 32 | private long offset; 33 | private long nextOffset; 34 | 35 | BasicFetchedMessage(TopicPartition topicPartition) { 36 | this.topicPartition = topicPartition; 37 | } 38 | 39 | void setPayload(ByteBuffer payload) { 40 | this.payload = payload; 41 | } 42 | 43 | void setOffset(long offset) { 44 | this.offset = offset; 45 | } 46 | 47 | void setNextOffset(long nextOffset) { 48 | this.nextOffset = nextOffset; 49 | } 50 | 51 | @Override 52 | public TopicPartition getTopicPartition() { 53 | return topicPartition; 54 | } 55 | 56 | @Override 57 | public ByteBuffer getPayload() { 58 | return payload; 59 | } 60 | 61 | @Override 62 | public long getOffset() { 63 | return offset; 64 | } 65 | 66 | @Override 67 | public long getNextOffset() { 68 | return nextOffset; 69 | } 70 | } 71 | -------------------------------------------------------------------------------- /twill-core/src/main/java/org/apache/twill/internal/kafka/client/ByteBufferEncoder.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Licensed to the Apache Software Foundation (ASF) under one 3 | * or more contributor license agreements. See the NOTICE file 4 | * distributed with this work for additional information 5 | * regarding copyright ownership. The ASF licenses this file 6 | * to you under the Apache License, Version 2.0 (the 7 | * "License"); you may not use this file except in compliance 8 | * with the License. You may obtain a copy of the License at 9 | * 10 | * http://www.apache.org/licenses/LICENSE-2.0 11 | * 12 | * Unless required by applicable law or agreed to in writing, software 13 | * distributed under the License is distributed on an "AS IS" BASIS, 14 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 15 | * See the License for the specific language governing permissions and 16 | * limitations under the License. 17 | */ 18 | package org.apache.twill.internal.kafka.client; 19 | 20 | import kafka.serializer.Encoder; 21 | import kafka.utils.VerifiableProperties; 22 | 23 | import java.nio.ByteBuffer; 24 | 25 | /** 26 | * A kafka {@link kafka.serializer.Encoder} for encoding byte buffer into byte array. 27 | */ 28 | public final class ByteBufferEncoder implements Encoder { 29 | 30 | public ByteBufferEncoder(VerifiableProperties properties) { 31 | } 32 | 33 | public byte[] toBytes(ByteBuffer buffer) { 34 | byte[] bytes = new byte[buffer.remaining()]; 35 | buffer.get(bytes); 36 | return bytes; 37 | } 38 | } 39 | -------------------------------------------------------------------------------- /twill-core/src/main/java/org/apache/twill/internal/kafka/client/IntegerEncoder.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Licensed to the Apache Software Foundation (ASF) under one 3 | * or more contributor license agreements. See the NOTICE file 4 | * distributed with this work for additional information 5 | * regarding copyright ownership. The ASF licenses this file 6 | * to you under the Apache License, Version 2.0 (the 7 | * "License"); you may not use this file except in compliance 8 | * with the License. You may obtain a copy of the License at 9 | * 10 | * http://www.apache.org/licenses/LICENSE-2.0 11 | * 12 | * Unless required by applicable law or agreed to in writing, software 13 | * distributed under the License is distributed on an "AS IS" BASIS, 14 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 15 | * See the License for the specific language governing permissions and 16 | * limitations under the License. 17 | */ 18 | package org.apache.twill.internal.kafka.client; 19 | 20 | import com.google.common.primitives.Ints; 21 | import kafka.serializer.Encoder; 22 | import kafka.utils.VerifiableProperties; 23 | 24 | /** 25 | * A kafka {@link kafka.serializer.Encoder} for encoding integer into bytes. 26 | */ 27 | public final class IntegerEncoder implements Encoder { 28 | 29 | public IntegerEncoder(VerifiableProperties properties) { 30 | } 31 | 32 | public byte[] toBytes(Integer buffer) { 33 | return Ints.toByteArray(buffer.intValue()); 34 | } 35 | } 36 | -------------------------------------------------------------------------------- /twill-core/src/main/java/org/apache/twill/internal/kafka/client/package-info.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Licensed to the Apache Software Foundation (ASF) under one 3 | * or more contributor license agreements. See the NOTICE file 4 | * distributed with this work for additional information 5 | * regarding copyright ownership. The ASF licenses this file 6 | * to you under the Apache License, Version 2.0 (the 7 | * "License"); you may not use this file except in compliance 8 | * with the License. You may obtain a copy of the License at 9 | * 10 | * http://www.apache.org/licenses/LICENSE-2.0 11 | * 12 | * Unless required by applicable law or agreed to in writing, software 13 | * distributed under the License is distributed on an "AS IS" BASIS, 14 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 15 | * See the License for the specific language governing permissions and 16 | * limitations under the License. 17 | */ 18 | /** 19 | * This package provides Kafka client implementation. 20 | */ 21 | package org.apache.twill.internal.kafka.client; 22 | -------------------------------------------------------------------------------- /twill-core/src/main/java/org/apache/twill/internal/logging/Loggings.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Licensed to the Apache Software Foundation (ASF) under one 3 | * or more contributor license agreements. See the NOTICE file 4 | * distributed with this work for additional information 5 | * regarding copyright ownership. The ASF licenses this file 6 | * to you under the Apache License, Version 2.0 (the 7 | * "License"); you may not use this file except in compliance 8 | * with the License. You may obtain a copy of the License at 9 | * 10 | * http://www.apache.org/licenses/LICENSE-2.0 11 | * 12 | * Unless required by applicable law or agreed to in writing, software 13 | * distributed under the License is distributed on an "AS IS" BASIS, 14 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 15 | * See the License for the specific language governing permissions and 16 | * limitations under the License. 17 | */ 18 | package org.apache.twill.internal.logging; 19 | 20 | import ch.qos.logback.classic.Logger; 21 | import ch.qos.logback.classic.LoggerContext; 22 | import ch.qos.logback.classic.spi.ILoggingEvent; 23 | import ch.qos.logback.core.Appender; 24 | import org.slf4j.ILoggerFactory; 25 | import org.slf4j.LoggerFactory; 26 | 27 | /** 28 | * 29 | */ 30 | public final class Loggings { 31 | 32 | public static void forceFlush() { 33 | ILoggerFactory loggerFactory = LoggerFactory.getILoggerFactory(); 34 | 35 | if (loggerFactory instanceof LoggerContext) { 36 | Appender appender = ((LoggerContext) loggerFactory).getLogger(Logger.ROOT_LOGGER_NAME) 37 | .getAppender("KAFKA"); 38 | if (appender != null && appender instanceof KafkaAppender) { 39 | ((KafkaAppender) appender).forceFlush(); 40 | } 41 | } 42 | } 43 | 44 | private Loggings() { 45 | } 46 | } 47 | -------------------------------------------------------------------------------- /twill-core/src/main/java/org/apache/twill/internal/package-info.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Licensed to the Apache Software Foundation (ASF) under one 3 | * or more contributor license agreements. See the NOTICE file 4 | * distributed with this work for additional information 5 | * regarding copyright ownership. The ASF licenses this file 6 | * to you under the Apache License, Version 2.0 (the 7 | * "License"); you may not use this file except in compliance 8 | * with the License. You may obtain a copy of the License at 9 | * 10 | * http://www.apache.org/licenses/LICENSE-2.0 11 | * 12 | * Unless required by applicable law or agreed to in writing, software 13 | * distributed under the License is distributed on an "AS IS" BASIS, 14 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 15 | * See the License for the specific language governing permissions and 16 | * limitations under the License. 17 | */ 18 | /** 19 | * This package provides internal classes for Twill. 20 | */ 21 | package org.apache.twill.internal; 22 | -------------------------------------------------------------------------------- /twill-core/src/main/java/org/apache/twill/internal/state/Message.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Licensed to the Apache Software Foundation (ASF) under one 3 | * or more contributor license agreements. See the NOTICE file 4 | * distributed with this work for additional information 5 | * regarding copyright ownership. The ASF licenses this file 6 | * to you under the Apache License, Version 2.0 (the 7 | * "License"); you may not use this file except in compliance 8 | * with the License. You may obtain a copy of the License at 9 | * 10 | * http://www.apache.org/licenses/LICENSE-2.0 11 | * 12 | * Unless required by applicable law or agreed to in writing, software 13 | * distributed under the License is distributed on an "AS IS" BASIS, 14 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 15 | * See the License for the specific language governing permissions and 16 | * limitations under the License. 17 | */ 18 | package org.apache.twill.internal.state; 19 | 20 | import org.apache.twill.api.Command; 21 | 22 | /** 23 | * Represent message objects to communicate between components in Twill. 24 | */ 25 | public interface Message { 26 | 27 | /** 28 | * Type of message. 29 | */ 30 | enum Type { 31 | SYSTEM, 32 | USER 33 | } 34 | 35 | /** 36 | * Scope of the message. 37 | */ 38 | enum Scope { 39 | APPLICATION, 40 | ALL_RUNNABLE, 41 | RUNNABLE, 42 | RUNNABLES 43 | } 44 | 45 | Type getType(); 46 | 47 | Scope getScope(); 48 | 49 | /** 50 | * @return the name of the target runnable if scope is {@link Scope#RUNNABLE} or {@code null} otherwise. 51 | */ 52 | String getRunnableName(); 53 | 54 | Command getCommand(); 55 | } 56 | -------------------------------------------------------------------------------- /twill-core/src/main/java/org/apache/twill/internal/state/MessageCallback.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Licensed to the Apache Software Foundation (ASF) under one 3 | * or more contributor license agreements. See the NOTICE file 4 | * distributed with this work for additional information 5 | * regarding copyright ownership. The ASF licenses this file 6 | * to you under the Apache License, Version 2.0 (the 7 | * "License"); you may not use this file except in compliance 8 | * with the License. You may obtain a copy of the License at 9 | * 10 | * http://www.apache.org/licenses/LICENSE-2.0 11 | * 12 | * Unless required by applicable law or agreed to in writing, software 13 | * distributed under the License is distributed on an "AS IS" BASIS, 14 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 15 | * See the License for the specific language governing permissions and 16 | * limitations under the License. 17 | */ 18 | package org.apache.twill.internal.state; 19 | 20 | import com.google.common.util.concurrent.ListenableFuture; 21 | 22 | /** 23 | * Define interface that could provide override-able callback method when a message is received. 24 | */ 25 | public interface MessageCallback { 26 | 27 | /** 28 | * Called when a message is received. 29 | * @param message Message being received. 30 | * @return A {@link ListenableFuture} that would be completed when message processing is completed or failed. 31 | * The result of the future should be the input message Id if succeeded. 32 | */ 33 | ListenableFuture onReceived(String messageId, Message message); 34 | } 35 | -------------------------------------------------------------------------------- /twill-core/src/main/java/org/apache/twill/internal/state/Messages.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Licensed to the Apache Software Foundation (ASF) under one 3 | * or more contributor license agreements. See the NOTICE file 4 | * distributed with this work for additional information 5 | * regarding copyright ownership. The ASF licenses this file 6 | * to you under the Apache License, Version 2.0 (the 7 | * "License"); you may not use this file except in compliance 8 | * with the License. You may obtain a copy of the License at 9 | * 10 | * http://www.apache.org/licenses/LICENSE-2.0 11 | * 12 | * Unless required by applicable law or agreed to in writing, software 13 | * distributed under the License is distributed on an "AS IS" BASIS, 14 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 15 | * See the License for the specific language governing permissions and 16 | * limitations under the License. 17 | */ 18 | package org.apache.twill.internal.state; 19 | 20 | import org.apache.twill.api.Command; 21 | 22 | /** 23 | * Factory class for creating instances of {@link Message}. 24 | */ 25 | public final class Messages { 26 | 27 | /** 28 | * Creates a {@link Message.Type#USER} type {@link Message} that sends the giving {@link Command} to a 29 | * particular runnable. 30 | * 31 | * @param runnableName Name of the runnable. 32 | * @param command The user command to send. 33 | * @return A new instance of {@link Message}. 34 | */ 35 | public static Message createForRunnable(String runnableName, Command command) { 36 | return new SimpleMessage(Message.Type.USER, Message.Scope.RUNNABLE, runnableName, command); 37 | } 38 | 39 | /** 40 | * Creates a {@link Message.Type#USER} type {@link Message} that sends the giving {@link Command} to all 41 | * runnables. 42 | * 43 | * @param command The user command to send. 44 | * @return A new instance of {@link Message}. 45 | */ 46 | public static Message createForAll(Command command) { 47 | return new SimpleMessage(Message.Type.USER, Message.Scope.ALL_RUNNABLE, null, command); 48 | } 49 | 50 | private Messages() { 51 | } 52 | } 53 | -------------------------------------------------------------------------------- /twill-core/src/main/java/org/apache/twill/internal/utils/Networks.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Licensed to the Apache Software Foundation (ASF) under one 3 | * or more contributor license agreements. See the NOTICE file 4 | * distributed with this work for additional information 5 | * regarding copyright ownership. The ASF licenses this file 6 | * to you under the Apache License, Version 2.0 (the 7 | * "License"); you may not use this file except in compliance 8 | * with the License. You may obtain a copy of the License at 9 | * 10 | * http://www.apache.org/licenses/LICENSE-2.0 11 | * 12 | * Unless required by applicable law or agreed to in writing, software 13 | * distributed under the License is distributed on an "AS IS" BASIS, 14 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 15 | * See the License for the specific language governing permissions and 16 | * limitations under the License. 17 | */ 18 | package org.apache.twill.internal.utils; 19 | 20 | import java.io.IOException; 21 | import java.net.ServerSocket; 22 | 23 | /** 24 | * 25 | */ 26 | public final class Networks { 27 | 28 | /** 29 | * Find a random free port in localhost for binding. 30 | * @return A port number or -1 for failure. 31 | */ 32 | public static int getRandomPort() { 33 | try (ServerSocket socket = new ServerSocket(0)) { 34 | return socket.getLocalPort(); 35 | } catch (IOException e) { 36 | return -1; 37 | } 38 | } 39 | 40 | private Networks() { 41 | } 42 | } 43 | -------------------------------------------------------------------------------- /twill-core/src/main/java/org/apache/twill/internal/utils/Resources.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Licensed to the Apache Software Foundation (ASF) under one 3 | * or more contributor license agreements. See the NOTICE file 4 | * distributed with this work for additional information 5 | * regarding copyright ownership. The ASF licenses this file 6 | * to you under the Apache License, Version 2.0 (the 7 | * "License"); you may not use this file except in compliance 8 | * with the License. You may obtain a copy of the License at 9 | * 10 | * http://www.apache.org/licenses/LICENSE-2.0 11 | * 12 | * Unless required by applicable law or agreed to in writing, software 13 | * distributed under the License is distributed on an "AS IS" BASIS, 14 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 15 | * See the License for the specific language governing permissions and 16 | * limitations under the License. 17 | */ 18 | package org.apache.twill.internal.utils; 19 | 20 | /** 21 | * Utility class to help adjusting container resource requirement. 22 | */ 23 | public final class Resources { 24 | 25 | /** 26 | * Computes the max heap size for a JVM process. 27 | * 28 | * @param containerMemory memory in MB of the container memory. 29 | * It is the maximum memory size allowed for the process. 30 | * @param nonHeapMemory memory in MB that needs to be reserved for non JVM heap memory for the process. 31 | * @param minHeapRatio minimum ratio for heap to non-heap memory. 32 | * @return memory in MB representing the max heap size for a JVM process. 33 | */ 34 | public static int computeMaxHeapSize(int containerMemory, int nonHeapMemory, double minHeapRatio) { 35 | if (((double) (containerMemory - nonHeapMemory) / containerMemory) >= minHeapRatio) { 36 | // Reduce -Xmx by the reserved memory size. 37 | return containerMemory - nonHeapMemory; 38 | } else { 39 | // If it is a small VM, just discount it by the min ratio. 40 | return (int) Math.ceil(containerMemory * minHeapRatio); 41 | } 42 | } 43 | 44 | private Resources() { 45 | } 46 | } 47 | -------------------------------------------------------------------------------- /twill-core/src/main/java/org/apache/twill/kafka/client/BrokerInfo.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Licensed to the Apache Software Foundation (ASF) under one 3 | * or more contributor license agreements. See the NOTICE file 4 | * distributed with this work for additional information 5 | * regarding copyright ownership. The ASF licenses this file 6 | * to you under the Apache License, Version 2.0 (the 7 | * "License"); you may not use this file except in compliance 8 | * with the License. You may obtain a copy of the License at 9 | * 10 | * http://www.apache.org/licenses/LICENSE-2.0 11 | * 12 | * Unless required by applicable law or agreed to in writing, software 13 | * distributed under the License is distributed on an "AS IS" BASIS, 14 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 15 | * See the License for the specific language governing permissions and 16 | * limitations under the License. 17 | */ 18 | package org.apache.twill.kafka.client; 19 | 20 | import com.google.common.base.Objects; 21 | 22 | /** 23 | * Represents broker information. This class should be serializable with Gson. 24 | */ 25 | public final class BrokerInfo { 26 | 27 | private String host; 28 | private int port; 29 | 30 | public String getHost() { 31 | return host; 32 | } 33 | 34 | public int getPort() { 35 | return port; 36 | } 37 | 38 | @Override 39 | public boolean equals(Object o) { 40 | if (this == o) { 41 | return true; 42 | } 43 | if (o == null || getClass() != o.getClass()) { 44 | return false; 45 | } 46 | 47 | BrokerInfo that = (BrokerInfo) o; 48 | return host.equals(that.getHost()) && port == that.getPort(); 49 | } 50 | 51 | @Override 52 | public int hashCode() { 53 | int result = host.hashCode(); 54 | result = 31 * result + port; 55 | return result; 56 | } 57 | 58 | @Override 59 | public String toString() { 60 | return Objects.toStringHelper(BrokerInfo.class) 61 | .add("host", host) 62 | .add("port", port) 63 | .toString(); 64 | } 65 | } 66 | -------------------------------------------------------------------------------- /twill-core/src/main/java/org/apache/twill/kafka/client/Compression.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Licensed to the Apache Software Foundation (ASF) under one 3 | * or more contributor license agreements. See the NOTICE file 4 | * distributed with this work for additional information 5 | * regarding copyright ownership. The ASF licenses this file 6 | * to you under the Apache License, Version 2.0 (the 7 | * "License"); you may not use this file except in compliance 8 | * with the License. You may obtain a copy of the License at 9 | * 10 | * http://www.apache.org/licenses/LICENSE-2.0 11 | * 12 | * Unless required by applicable law or agreed to in writing, software 13 | * distributed under the License is distributed on an "AS IS" BASIS, 14 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 15 | * See the License for the specific language governing permissions and 16 | * limitations under the License. 17 | */ 18 | package org.apache.twill.kafka.client; 19 | 20 | /** 21 | * An Enum representing different compression codec options for publishing messages. 22 | */ 23 | public enum Compression { 24 | 25 | NONE, 26 | GZIP, 27 | SNAPPY; 28 | 29 | private final String codec; 30 | 31 | Compression() { 32 | this.codec = name().toLowerCase(); 33 | } 34 | 35 | public String getCodec() { 36 | return codec; 37 | } 38 | } 39 | -------------------------------------------------------------------------------- /twill-core/src/main/java/org/apache/twill/kafka/client/FetchedMessage.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Licensed to the Apache Software Foundation (ASF) under one 3 | * or more contributor license agreements. See the NOTICE file 4 | * distributed with this work for additional information 5 | * regarding copyright ownership. The ASF licenses this file 6 | * to you under the Apache License, Version 2.0 (the 7 | * "License"); you may not use this file except in compliance 8 | * with the License. You may obtain a copy of the License at 9 | * 10 | * http://www.apache.org/licenses/LICENSE-2.0 11 | * 12 | * Unless required by applicable law or agreed to in writing, software 13 | * distributed under the License is distributed on an "AS IS" BASIS, 14 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 15 | * See the License for the specific language governing permissions and 16 | * limitations under the License. 17 | */ 18 | package org.apache.twill.kafka.client; 19 | 20 | import java.nio.ByteBuffer; 21 | 22 | /** 23 | * 24 | */ 25 | public interface FetchedMessage { 26 | 27 | TopicPartition getTopicPartition(); 28 | 29 | ByteBuffer getPayload(); 30 | 31 | /** 32 | * Returns the offset for the current message. 33 | */ 34 | long getOffset(); 35 | 36 | /** 37 | * Returns the offset for the next message to be read. 38 | */ 39 | long getNextOffset(); 40 | } 41 | -------------------------------------------------------------------------------- /twill-core/src/main/java/org/apache/twill/kafka/client/KafkaClient.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Licensed to the Apache Software Foundation (ASF) under one 3 | * or more contributor license agreements. See the NOTICE file 4 | * distributed with this work for additional information 5 | * regarding copyright ownership. The ASF licenses this file 6 | * to you under the Apache License, Version 2.0 (the 7 | * "License"); you may not use this file except in compliance 8 | * with the License. You may obtain a copy of the License at 9 | * 10 | * http://www.apache.org/licenses/LICENSE-2.0 11 | * 12 | * Unless required by applicable law or agreed to in writing, software 13 | * distributed under the License is distributed on an "AS IS" BASIS, 14 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 15 | * See the License for the specific language governing permissions and 16 | * limitations under the License. 17 | */ 18 | package org.apache.twill.kafka.client; 19 | 20 | /** 21 | * Represents a kafka client that can publish/subscribe to a Kafka server cluster. 22 | */ 23 | public interface KafkaClient { 24 | 25 | /** 26 | * Creates a {@link KafkaPublisher} that is ready for publish. 27 | * @param ack Type of ack that the publisher would use for all it's publish. 28 | * @param compression The compression type for messages published through the returned publisher. 29 | * @return A {@link KafkaPublisher}. 30 | */ 31 | KafkaPublisher getPublisher(KafkaPublisher.Ack ack, Compression compression); 32 | 33 | /** 34 | * Creates a {@link KafkaConsumer} for consuming messages. 35 | * @return A {@link KafkaConsumer}. 36 | */ 37 | KafkaConsumer getConsumer(); 38 | } 39 | -------------------------------------------------------------------------------- /twill-core/src/main/java/org/apache/twill/kafka/client/KafkaClientService.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Licensed to the Apache Software Foundation (ASF) under one 3 | * or more contributor license agreements. See the NOTICE file 4 | * distributed with this work for additional information 5 | * regarding copyright ownership. The ASF licenses this file 6 | * to you under the Apache License, Version 2.0 (the 7 | * "License"); you may not use this file except in compliance 8 | * with the License. You may obtain a copy of the License at 9 | * 10 | * http://www.apache.org/licenses/LICENSE-2.0 11 | * 12 | * Unless required by applicable law or agreed to in writing, software 13 | * distributed under the License is distributed on an "AS IS" BASIS, 14 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 15 | * See the License for the specific language governing permissions and 16 | * limitations under the License. 17 | */ 18 | package org.apache.twill.kafka.client; 19 | 20 | import com.google.common.util.concurrent.Service; 21 | 22 | /** 23 | * A {@link KafkaClient} that extends {@link Service} to provide lifecycle management. 24 | */ 25 | public interface KafkaClientService extends KafkaClient, Service { 26 | } 27 | -------------------------------------------------------------------------------- /twill-core/src/main/java/org/apache/twill/kafka/client/TopicPartition.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Licensed to the Apache Software Foundation (ASF) under one 3 | * or more contributor license agreements. See the NOTICE file 4 | * distributed with this work for additional information 5 | * regarding copyright ownership. The ASF licenses this file 6 | * to you under the Apache License, Version 2.0 (the 7 | * "License"); you may not use this file except in compliance 8 | * with the License. You may obtain a copy of the License at 9 | * 10 | * http://www.apache.org/licenses/LICENSE-2.0 11 | * 12 | * Unless required by applicable law or agreed to in writing, software 13 | * distributed under the License is distributed on an "AS IS" BASIS, 14 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 15 | * See the License for the specific language governing permissions and 16 | * limitations under the License. 17 | */ 18 | package org.apache.twill.kafka.client; 19 | 20 | import com.google.common.base.Objects; 21 | 22 | /** 23 | * Represents a combination of topic and partition. 24 | */ 25 | public class TopicPartition { 26 | 27 | private final String topic; 28 | private final int partition; 29 | 30 | public TopicPartition(String topic, int partition) { 31 | this.topic = topic; 32 | this.partition = partition; 33 | } 34 | 35 | public final String getTopic() { 36 | return topic; 37 | } 38 | 39 | public final int getPartition() { 40 | return partition; 41 | } 42 | 43 | @Override 44 | public boolean equals(Object o) { 45 | if (this == o) { 46 | return true; 47 | } 48 | if (o == null || getClass() != o.getClass()) { 49 | return false; 50 | } 51 | 52 | TopicPartition other = (TopicPartition) o; 53 | return partition == other.partition && topic.equals(other.topic); 54 | } 55 | 56 | @Override 57 | public int hashCode() { 58 | int result = topic.hashCode(); 59 | result = 31 * result + partition; 60 | return result; 61 | } 62 | 63 | @Override 64 | public String toString() { 65 | return Objects.toStringHelper(this) 66 | .add("topic", topic) 67 | .add("partition", partition) 68 | .toString(); 69 | } 70 | } 71 | -------------------------------------------------------------------------------- /twill-core/src/main/java/org/apache/twill/kafka/client/package-info.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Licensed to the Apache Software Foundation (ASF) under one 3 | * or more contributor license agreements. See the NOTICE file 4 | * distributed with this work for additional information 5 | * regarding copyright ownership. The ASF licenses this file 6 | * to you under the Apache License, Version 2.0 (the 7 | * "License"); you may not use this file except in compliance 8 | * with the License. You may obtain a copy of the License at 9 | * 10 | * http://www.apache.org/licenses/LICENSE-2.0 11 | * 12 | * Unless required by applicable law or agreed to in writing, software 13 | * distributed under the License is distributed on an "AS IS" BASIS, 14 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 15 | * See the License for the specific language governing permissions and 16 | * limitations under the License. 17 | */ 18 | /** 19 | * This package provides Kafka client interfaces. 20 | */ 21 | package org.apache.twill.kafka.client; 22 | -------------------------------------------------------------------------------- /twill-core/src/main/java/org/apache/twill/launcher/FindFreePort.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Licensed to the Apache Software Foundation (ASF) under one 3 | * or more contributor license agreements. See the NOTICE file 4 | * distributed with this work for additional information 5 | * regarding copyright ownership. The ASF licenses this file 6 | * to you under the Apache License, Version 2.0 (the 7 | * "License"); you may not use this file except in compliance 8 | * with the License. You may obtain a copy of the License at 9 | * 10 | * http://www.apache.org/licenses/LICENSE-2.0 11 | * 12 | * Unless required by applicable law or agreed to in writing, software 13 | * distributed under the License is distributed on an "AS IS" BASIS, 14 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 15 | * See the License for the specific language governing permissions and 16 | * limitations under the License. 17 | */ 18 | package org.apache.twill.launcher; 19 | 20 | import java.net.ServerSocket; 21 | 22 | /** 23 | * Utility main class to find a free port on a machine. 24 | */ 25 | public class FindFreePort { 26 | 27 | public static void main(String[] args) throws Exception { 28 | try (ServerSocket socket = new ServerSocket(0)) { 29 | System.out.println(socket.getLocalPort()); 30 | } 31 | } 32 | 33 | } 34 | -------------------------------------------------------------------------------- /twill-core/src/test/java/org/apache/twill/internal/utils/Class1.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Licensed to the Apache Software Foundation (ASF) under one 3 | * or more contributor license agreements. See the NOTICE file 4 | * distributed with this work for additional information 5 | * regarding copyright ownership. The ASF licenses this file 6 | * to you under the Apache License, Version 2.0 (the 7 | * "License"); you may not use this file except in compliance 8 | * with the License. You may obtain a copy of the License at 9 | * 10 | * http://www.apache.org/licenses/LICENSE-2.0 11 | * 12 | * Unless required by applicable law or agreed to in writing, software 13 | * distributed under the License is distributed on an "AS IS" BASIS, 14 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 15 | * See the License for the specific language governing permissions and 16 | * limitations under the License. 17 | */ 18 | package org.apache.twill.internal.utils; 19 | 20 | /** Dummy class used in ApplicationBundlerTest. */ 21 | public class Class1 { 22 | 23 | } 24 | -------------------------------------------------------------------------------- /twill-core/src/test/java/org/apache/twill/internal/utils/Class2.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Licensed to the Apache Software Foundation (ASF) under one 3 | * or more contributor license agreements. See the NOTICE file 4 | * distributed with this work for additional information 5 | * regarding copyright ownership. The ASF licenses this file 6 | * to you under the Apache License, Version 2.0 (the 7 | * "License"); you may not use this file except in compliance 8 | * with the License. You may obtain a copy of the License at 9 | * 10 | * http://www.apache.org/licenses/LICENSE-2.0 11 | * 12 | * Unless required by applicable law or agreed to in writing, software 13 | * distributed under the License is distributed on an "AS IS" BASIS, 14 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 15 | * See the License for the specific language governing permissions and 16 | * limitations under the License. 17 | */ 18 | package org.apache.twill.internal.utils; 19 | 20 | /** Dummy class used in ApplicationBundlerTest. */ 21 | public class Class2 { 22 | 23 | } 24 | -------------------------------------------------------------------------------- /twill-core/src/test/resources/logback-test.xml: -------------------------------------------------------------------------------- 1 | 2 | 18 | 19 | 20 | 21 | 22 | 23 | %d{ISO8601} - %-5p [%t:%C{1}@%L] - %m%n 24 | 25 | 26 | 27 | 28 | 29 | 30 | 31 | 32 | 33 | 34 | 35 | -------------------------------------------------------------------------------- /twill-discovery-api/pom.xml: -------------------------------------------------------------------------------- 1 | 2 | 19 | 22 | 23 | twill-parent 24 | org.apache.twill 25 | 0.15.0-SNAPSHOT 26 | 27 | 4.0.0 28 | 29 | twill-discovery-api 30 | Apache Twill discovery service API 31 | 32 | 33 | 34 | ${project.groupId} 35 | twill-common 36 | ${project.version} 37 | 38 | 39 | junit 40 | junit 41 | 42 | 43 | 44 | -------------------------------------------------------------------------------- /twill-discovery-api/src/main/java/org/apache/twill/discovery/DiscoveryService.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Licensed to the Apache Software Foundation (ASF) under one 3 | * or more contributor license agreements. See the NOTICE file 4 | * distributed with this work for additional information 5 | * regarding copyright ownership. The ASF licenses this file 6 | * to you under the Apache License, Version 2.0 (the 7 | * "License"); you may not use this file except in compliance 8 | * with the License. You may obtain a copy of the License at 9 | * 10 | * http://www.apache.org/licenses/LICENSE-2.0 11 | * 12 | * Unless required by applicable law or agreed to in writing, software 13 | * distributed under the License is distributed on an "AS IS" BASIS, 14 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 15 | * See the License for the specific language governing permissions and 16 | * limitations under the License. 17 | */ 18 | package org.apache.twill.discovery; 19 | 20 | 21 | import org.apache.twill.common.Cancellable; 22 | 23 | /** 24 | * DiscoveryService defines interface for registering {@link Discoverable}. 25 | */ 26 | public interface DiscoveryService { 27 | 28 | /** 29 | * Registers a {@link Discoverable} service. 30 | * @param discoverable Information of the service provider that could be discovered. 31 | * @return A {@link Cancellable} for un-registration. 32 | */ 33 | Cancellable register(Discoverable discoverable); 34 | } 35 | 36 | -------------------------------------------------------------------------------- /twill-discovery-api/src/main/java/org/apache/twill/discovery/DiscoveryServiceClient.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Licensed to the Apache Software Foundation (ASF) under one 3 | * or more contributor license agreements. See the NOTICE file 4 | * distributed with this work for additional information 5 | * regarding copyright ownership. The ASF licenses this file 6 | * to you under the Apache License, Version 2.0 (the 7 | * "License"); you may not use this file except in compliance 8 | * with the License. You may obtain a copy of the License at 9 | * 10 | * http://www.apache.org/licenses/LICENSE-2.0 11 | * 12 | * Unless required by applicable law or agreed to in writing, software 13 | * distributed under the License is distributed on an "AS IS" BASIS, 14 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 15 | * See the License for the specific language governing permissions and 16 | * limitations under the License. 17 | */ 18 | package org.apache.twill.discovery; 19 | 20 | /** 21 | * Interface for {@link DiscoveryServiceClient} to discover services registered with {@link DiscoveryService}. 22 | */ 23 | public interface DiscoveryServiceClient { 24 | 25 | /** 26 | * Discover service with the given name. 27 | * 28 | * @param name Name of the service 29 | * @return A {@link ServiceDiscovered} object representing the result. 30 | */ 31 | ServiceDiscovered discover(String name); 32 | } 33 | -------------------------------------------------------------------------------- /twill-discovery-core/pom.xml: -------------------------------------------------------------------------------- 1 | 2 | 19 | 22 | 23 | twill-parent 24 | org.apache.twill 25 | 0.15.0-SNAPSHOT 26 | 27 | 4.0.0 28 | 29 | twill-discovery-core 30 | Apache Twill discovery service implementations 31 | 32 | 33 | 34 | ${project.groupId} 35 | twill-discovery-api 36 | ${project.version} 37 | 38 | 39 | ${project.groupId} 40 | twill-zookeeper 41 | ${project.version} 42 | 43 | 44 | com.google.code.gson 45 | gson 46 | 47 | 48 | junit 49 | junit 50 | 51 | 52 | 53 | -------------------------------------------------------------------------------- /twill-discovery-core/src/main/java/org/apache/twill/discovery/package-info.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Licensed to the Apache Software Foundation (ASF) under one 3 | * or more contributor license agreements. See the NOTICE file 4 | * distributed with this work for additional information 5 | * regarding copyright ownership. The ASF licenses this file 6 | * to you under the Apache License, Version 2.0 (the 7 | * "License"); you may not use this file except in compliance 8 | * with the License. You may obtain a copy of the License at 9 | * 10 | * http://www.apache.org/licenses/LICENSE-2.0 11 | * 12 | * Unless required by applicable law or agreed to in writing, software 13 | * distributed under the License is distributed on an "AS IS" BASIS, 14 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 15 | * See the License for the specific language governing permissions and 16 | * limitations under the License. 17 | */ 18 | /** 19 | * Classes in this package provides service discovery implementations. 20 | */ 21 | package org.apache.twill.discovery; 22 | -------------------------------------------------------------------------------- /twill-discovery-core/src/test/java/org/apache/twill/discovery/InMemoryDiscoveryServiceTest.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Licensed to the Apache Software Foundation (ASF) under one 3 | * or more contributor license agreements. See the NOTICE file 4 | * distributed with this work for additional information 5 | * regarding copyright ownership. The ASF licenses this file 6 | * to you under the Apache License, Version 2.0 (the 7 | * "License"); you may not use this file except in compliance 8 | * with the License. You may obtain a copy of the License at 9 | * 10 | * http://www.apache.org/licenses/LICENSE-2.0 11 | * 12 | * Unless required by applicable law or agreed to in writing, software 13 | * distributed under the License is distributed on an "AS IS" BASIS, 14 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 15 | * See the License for the specific language governing permissions and 16 | * limitations under the License. 17 | */ 18 | 19 | package org.apache.twill.discovery; 20 | 21 | import com.google.common.collect.Maps; 22 | 23 | import java.util.Map; 24 | 25 | /** 26 | * Test memory based service discovery service. 27 | */ 28 | public class InMemoryDiscoveryServiceTest extends DiscoveryServiceTestBase { 29 | 30 | @Override 31 | protected Map.Entry create() { 32 | DiscoveryService discoveryService = new InMemoryDiscoveryService(); 33 | return Maps.immutableEntry(discoveryService, (DiscoveryServiceClient) discoveryService); 34 | } 35 | } 36 | -------------------------------------------------------------------------------- /twill-discovery-core/src/test/resources/logback-test.xml: -------------------------------------------------------------------------------- 1 | 2 | 18 | 19 | 20 | 21 | 22 | 23 | %d{ISO8601} - %-5p [%t:%C{1}@%L] - %m%n 24 | 25 | 26 | 27 | 28 | 29 | 30 | 31 | 32 | 33 | 34 | -------------------------------------------------------------------------------- /twill-examples/echo/src/main/java/echo/EchoMain.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Licensed to the Apache Software Foundation (ASF) under one 3 | * or more contributor license agreements. See the NOTICE file 4 | * distributed with this work for additional information 5 | * regarding copyright ownership. The ASF licenses this file 6 | * to you under the Apache License, Version 2.0 (the 7 | * "License"); you may not use this file except in compliance 8 | * with the License. You may obtain a copy of the License at 9 | * 10 | * http://www.apache.org/licenses/LICENSE-2.0 11 | * 12 | * Unless required by applicable law or agreed to in writing, software 13 | * distributed under the License is distributed on an "AS IS" BASIS, 14 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 15 | * See the License for the specific language governing permissions and 16 | * limitations under the License. 17 | */ 18 | package echo; 19 | 20 | import com.google.common.base.Converter; 21 | import org.slf4j.Logger; 22 | import org.slf4j.LoggerFactory; 23 | 24 | import java.util.Arrays; 25 | 26 | /** 27 | * Simple application used for BundledJarExample and RuntimeBundledJarExample. 28 | */ 29 | public class EchoMain { 30 | private static final Logger logger = LoggerFactory.getLogger(EchoMain.class); 31 | 32 | public static void main(String[] args) { 33 | logger.info("Hello from EchoMain: " + new TestConverter().convert("sdflkj")); 34 | System.err.println("err HELLO from scatch"); 35 | System.out.println("out HELLO from scatch"); 36 | logger.info("Got args: " + Arrays.toString(args)); 37 | System.out.println("Got args: " + Arrays.toString(args)); 38 | } 39 | 40 | private static final class TestConverter extends Converter { 41 | 42 | @Override 43 | protected Integer doForward(String s) { 44 | return s.length(); 45 | } 46 | 47 | @Override 48 | protected String doBackward(Integer integer) { 49 | return integer.toString(); 50 | } 51 | } 52 | 53 | } 54 | -------------------------------------------------------------------------------- /twill-ext/pom.xml: -------------------------------------------------------------------------------- 1 | 2 | 19 | 22 | 23 | twill-parent 24 | org.apache.twill 25 | 0.15.0-SNAPSHOT 26 | 27 | 28 | 4.0.0 29 | twill-ext 30 | Apache Twill extensions 31 | 32 | 33 | 34 | ${project.groupId} 35 | twill-yarn 36 | ${project.version} 37 | 38 | 39 | ${project.groupId} 40 | twill-core 41 | ${project.version} 42 | 43 | 44 | com.google.guava 45 | guava 46 | 47 | 48 | com.google.code.gson 49 | gson 50 | 51 | 52 | junit 53 | junit 54 | 55 | 56 | 57 | 58 | -------------------------------------------------------------------------------- /twill-yarn/src/main/hadoop21/org/apache/twill/internal/yarn/Hadoop21YarnContainerInfo.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Licensed to the Apache Software Foundation (ASF) under one 3 | * or more contributor license agreements. See the NOTICE file 4 | * distributed with this work for additional information 5 | * regarding copyright ownership. The ASF licenses this file 6 | * to you under the Apache License, Version 2.0 (the 7 | * "License"); you may not use this file except in compliance 8 | * with the License. You may obtain a copy of the License at 9 | * 10 | * http://www.apache.org/licenses/LICENSE-2.0 11 | * 12 | * Unless required by applicable law or agreed to in writing, software 13 | * distributed under the License is distributed on an "AS IS" BASIS, 14 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 15 | * See the License for the specific language governing permissions and 16 | * limitations under the License. 17 | */ 18 | package org.apache.twill.internal.yarn; 19 | 20 | import com.google.common.base.Throwables; 21 | import org.apache.hadoop.yarn.api.records.Container; 22 | 23 | import java.net.InetAddress; 24 | import java.net.UnknownHostException; 25 | 26 | /** 27 | * Implementation of {@link YarnContainerInfo} for Apache Hadoop 2.1.0 or above. 28 | */ 29 | public final class Hadoop21YarnContainerInfo implements YarnContainerInfo { 30 | 31 | private final Container container; 32 | 33 | public Hadoop21YarnContainerInfo(Container container) { 34 | this.container = container; 35 | } 36 | 37 | @Override 38 | public T getContainer() { 39 | return (T) container; 40 | } 41 | 42 | @Override 43 | public String getId() { 44 | return container.getId().toString(); 45 | } 46 | 47 | @Override 48 | public InetAddress getHost() { 49 | try { 50 | return InetAddress.getByName(container.getNodeId().getHost()); 51 | } catch (UnknownHostException e) { 52 | throw Throwables.propagate(e); 53 | } 54 | } 55 | 56 | @Override 57 | public int getPort() { 58 | return container.getNodeId().getPort(); 59 | } 60 | 61 | @Override 62 | public int getMemoryMB() { 63 | return container.getResource().getMemory(); 64 | } 65 | 66 | @Override 67 | public int getVirtualCores() { 68 | return container.getResource().getVirtualCores(); 69 | } 70 | } 71 | -------------------------------------------------------------------------------- /twill-yarn/src/main/hadoop21/org/apache/twill/internal/yarn/Hadoop21YarnContainerStatus.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Licensed to the Apache Software Foundation (ASF) under one 3 | * or more contributor license agreements. See the NOTICE file 4 | * distributed with this work for additional information 5 | * regarding copyright ownership. The ASF licenses this file 6 | * to you under the Apache License, Version 2.0 (the 7 | * "License"); you may not use this file except in compliance 8 | * with the License. You may obtain a copy of the License at 9 | * 10 | * http://www.apache.org/licenses/LICENSE-2.0 11 | * 12 | * Unless required by applicable law or agreed to in writing, software 13 | * distributed under the License is distributed on an "AS IS" BASIS, 14 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 15 | * See the License for the specific language governing permissions and 16 | * limitations under the License. 17 | */ 18 | package org.apache.twill.internal.yarn; 19 | 20 | import org.apache.hadoop.yarn.api.records.ContainerState; 21 | import org.apache.hadoop.yarn.api.records.ContainerStatus; 22 | 23 | /** 24 | * 25 | */ 26 | public final class Hadoop21YarnContainerStatus implements YarnContainerStatus { 27 | 28 | private final ContainerStatus containerStatus; 29 | 30 | public Hadoop21YarnContainerStatus(ContainerStatus containerStatus) { 31 | this.containerStatus = containerStatus; 32 | } 33 | 34 | @Override 35 | public String getContainerId() { 36 | return containerStatus.getContainerId().toString(); 37 | } 38 | 39 | @Override 40 | public ContainerState getState() { 41 | return containerStatus.getState(); 42 | } 43 | 44 | @Override 45 | public int getExitStatus() { 46 | return containerStatus.getExitStatus(); 47 | } 48 | 49 | @Override 50 | public String getDiagnostics() { 51 | return containerStatus.getDiagnostics(); 52 | } 53 | } 54 | -------------------------------------------------------------------------------- /twill-yarn/src/main/hadoop22/org/apache/twill/internal/yarn/Hadoop22YarnAMClient.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Licensed to the Apache Software Foundation (ASF) under one 3 | * or more contributor license agreements. See the NOTICE file 4 | * distributed with this work for additional information 5 | * regarding copyright ownership. The ASF licenses this file 6 | * to you under the Apache License, Version 2.0 (the 7 | * "License"); you may not use this file except in compliance 8 | * with the License. You may obtain a copy of the License at 9 | * 10 | * http://www.apache.org/licenses/LICENSE-2.0 11 | * 12 | * Unless required by applicable law or agreed to in writing, software 13 | * distributed under the License is distributed on an "AS IS" BASIS, 14 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 15 | * See the License for the specific language governing permissions and 16 | * limitations under the License. 17 | */ 18 | package org.apache.twill.internal.yarn; 19 | 20 | import org.apache.hadoop.conf.Configuration; 21 | import org.slf4j.Logger; 22 | import org.slf4j.LoggerFactory; 23 | 24 | import java.util.List; 25 | 26 | /** 27 | * Wrapper class for AMRMClient for Hadoop version 2.2 or greater. 28 | */ 29 | public class Hadoop22YarnAMClient extends Hadoop21YarnAMClient { 30 | 31 | private static final Logger LOG = LoggerFactory.getLogger(Hadoop22YarnAMClient.class); 32 | 33 | public Hadoop22YarnAMClient(Configuration conf) { 34 | super(conf); 35 | } 36 | 37 | @Override 38 | protected void updateBlacklist(List blacklistAdditions, List blacklistRemovals) { 39 | LOG.debug("Blacklist Additions: {} , Blacklist Removals: {}", blacklistAdditions, blacklistRemovals); 40 | amrmClient.updateBlacklist(blacklistAdditions, blacklistRemovals); 41 | } 42 | } 43 | -------------------------------------------------------------------------------- /twill-yarn/src/main/hadoop26/org/apache/twill/internal/yarn/Hadoop26YarnAMClient.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Licensed to the Apache Software Foundation (ASF) under one 3 | * or more contributor license agreements. See the NOTICE file 4 | * distributed with this work for additional information 5 | * regarding copyright ownership. The ASF licenses this file 6 | * to you under the Apache License, Version 2.0 (the 7 | * "License"); you may not use this file except in compliance 8 | * with the License. You may obtain a copy of the License at 9 | * 10 | * http://www.apache.org/licenses/LICENSE-2.0 11 | * 12 | * Unless required by applicable law or agreed to in writing, software 13 | * distributed under the License is distributed on an "AS IS" BASIS, 14 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 15 | * See the License for the specific language governing permissions and 16 | * limitations under the License. 17 | */ 18 | package org.apache.twill.internal.yarn; 19 | 20 | import org.apache.hadoop.conf.Configuration; 21 | import org.apache.hadoop.yarn.api.records.ContainerId; 22 | import org.slf4j.Logger; 23 | import org.slf4j.LoggerFactory; 24 | 25 | import java.util.List; 26 | 27 | /** 28 | * Wrapper class for AMRMClient for Hadoop version 2.6 or greater. 29 | */ 30 | public final class Hadoop26YarnAMClient extends Hadoop22YarnAMClient { 31 | 32 | private static final Logger LOG = LoggerFactory.getLogger(Hadoop26YarnAMClient.class); 33 | 34 | public Hadoop26YarnAMClient(Configuration conf) { 35 | super(conf); 36 | } 37 | 38 | @Override 39 | protected final ContainerId containerIdLookup(String containerIdStr) { 40 | return (ContainerId.fromString(containerIdStr)); 41 | } 42 | } 43 | -------------------------------------------------------------------------------- /twill-yarn/src/main/hadoop26/org/apache/twill/internal/yarn/Hadoop26YarnAppClient.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Licensed to the Apache Software Foundation (ASF) under one 3 | * or more contributor license agreements. See the NOTICE file 4 | * distributed with this work for additional information 5 | * regarding copyright ownership. The ASF licenses this file 6 | * to you under the Apache License, Version 2.0 (the 7 | * "License"); you may not use this file except in compliance 8 | * with the License. You may obtain a copy of the License at 9 | * 10 | * http://www.apache.org/licenses/LICENSE-2.0 11 | * 12 | * Unless required by applicable law or agreed to in writing, software 13 | * distributed under the License is distributed on an "AS IS" BASIS, 14 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 15 | * See the License for the specific language governing permissions and 16 | * limitations under the License. 17 | */ 18 | 19 | package org.apache.twill.internal.yarn; 20 | 21 | import org.apache.hadoop.conf.Configuration; 22 | import org.apache.hadoop.yarn.api.records.ApplicationSubmissionContext; 23 | import org.apache.twill.api.Configs; 24 | 25 | /** 26 | *

27 | * The service implementation of {@link YarnAppClient} for Apache Hadoop 2.6 and beyond. 28 | * 29 | * The {@link VersionDetectYarnAppClientFactory} class will decide to return instance of this class for 30 | * Apache Hadoop 2.6 and beyond. 31 | *

32 | */ 33 | @SuppressWarnings("unused") 34 | public class Hadoop26YarnAppClient extends Hadoop23YarnAppClient { 35 | 36 | public Hadoop26YarnAppClient(Configuration configuration) { 37 | super(configuration); 38 | } 39 | 40 | @Override 41 | protected void configureAppSubmissionContext(ApplicationSubmissionContext context) { 42 | super.configureAppSubmissionContext(context); 43 | long interval = configuration.getLong(Configs.Keys.YARN_ATTEMPT_FAILURES_VALIDITY_INTERVAL, -1L); 44 | if (interval > 0) { 45 | context.setAttemptFailuresValidityInterval(interval); 46 | } 47 | } 48 | } 49 | -------------------------------------------------------------------------------- /twill-yarn/src/main/java/org/apache/twill/filesystem/package-info.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Licensed to the Apache Software Foundation (ASF) under one 3 | * or more contributor license agreements. See the NOTICE file 4 | * distributed with this work for additional information 5 | * regarding copyright ownership. The ASF licenses this file 6 | * to you under the Apache License, Version 2.0 (the 7 | * "License"); you may not use this file except in compliance 8 | * with the License. You may obtain a copy of the License at 9 | * 10 | * http://www.apache.org/licenses/LICENSE-2.0 11 | * 12 | * Unless required by applicable law or agreed to in writing, software 13 | * distributed under the License is distributed on an "AS IS" BASIS, 14 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 15 | * See the License for the specific language governing permissions and 16 | * limitations under the License. 17 | */ 18 | /** 19 | * Contains HDFS location classes. 20 | */ 21 | package org.apache.twill.filesystem; 22 | -------------------------------------------------------------------------------- /twill-yarn/src/main/java/org/apache/twill/internal/ContainerExitCodes.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Licensed to the Apache Software Foundation (ASF) under one 3 | * or more contributor license agreements. See the NOTICE file 4 | * distributed with this work for additional information 5 | * regarding copyright ownership. The ASF licenses this file 6 | * to you under the Apache License, Version 2.0 (the 7 | * "License"); you may not use this file except in compliance 8 | * with the License. You may obtain a copy of the License at 9 | * 10 | * http://www.apache.org/licenses/LICENSE-2.0 11 | * 12 | * Unless required by applicable law or agreed to in writing, software 13 | * distributed under the License is distributed on an "AS IS" BASIS, 14 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 15 | * See the License for the specific language governing permissions and 16 | * limitations under the License. 17 | */ 18 | package org.apache.twill.internal; 19 | 20 | /** 21 | * Collection of known exit code. Some of the codes are copied from ContainerExitStatus as that class is missing in 22 | * older YARN version. 23 | */ 24 | public final class ContainerExitCodes { 25 | 26 | public static final int SUCCESS = 0; 27 | 28 | /** 29 | * When the container exit when it fails to initialize. 30 | */ 31 | public static final int INIT_FAILED = 10; 32 | 33 | public static final int INVALID = -1000; 34 | 35 | /** 36 | * Containers killed by the framework, either due to being released by 37 | * the application or being 'lost' due to node failures etc. 38 | */ 39 | public static final int ABORTED = -100; 40 | 41 | /** 42 | * When threshold number of the nodemanager-local-directories or 43 | * threshold number of the nodemanager-log-directories become bad. 44 | */ 45 | public static final int DISKS_FAILED = -101; 46 | 47 | /** 48 | * Containers preempted by the YARN framework. 49 | */ 50 | public static final int PREEMPTED = -102; 51 | } 52 | -------------------------------------------------------------------------------- /twill-yarn/src/main/java/org/apache/twill/internal/appmaster/ApplicationMasterInfo.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Licensed to the Apache Software Foundation (ASF) under one 3 | * or more contributor license agreements. See the NOTICE file 4 | * distributed with this work for additional information 5 | * regarding copyright ownership. The ASF licenses this file 6 | * to you under the Apache License, Version 2.0 (the 7 | * "License"); you may not use this file except in compliance 8 | * with the License. You may obtain a copy of the License at 9 | * 10 | * http://www.apache.org/licenses/LICENSE-2.0 11 | * 12 | * Unless required by applicable law or agreed to in writing, software 13 | * distributed under the License is distributed on an "AS IS" BASIS, 14 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 15 | * See the License for the specific language governing permissions and 16 | * limitations under the License. 17 | */ 18 | package org.apache.twill.internal.appmaster; 19 | 20 | import org.apache.hadoop.yarn.api.records.ApplicationId; 21 | import org.apache.twill.internal.ResourceCapability; 22 | 23 | /** 24 | * Represents information of the application master. 25 | */ 26 | public class ApplicationMasterInfo implements ResourceCapability { 27 | 28 | private final ApplicationId appId; 29 | private final int memoryMB; 30 | private final int virtualCores; 31 | 32 | public ApplicationMasterInfo(ApplicationId appId, int memoryMB, int virtualCores) { 33 | this.appId = appId; 34 | this.memoryMB = memoryMB; 35 | this.virtualCores = virtualCores; 36 | } 37 | 38 | /** 39 | * Returns the application ID for the YARN application. 40 | */ 41 | public ApplicationId getAppId() { 42 | return appId; 43 | } 44 | 45 | @Override 46 | public int getMemoryMB() { 47 | return memoryMB; 48 | } 49 | 50 | @Override 51 | public int getVirtualCores() { 52 | return virtualCores; 53 | } 54 | 55 | @Override 56 | public String toString() { 57 | return "ApplicationMasterInfo{" + 58 | "appId=" + appId + 59 | ", memoryMB=" + memoryMB + 60 | ", virtualCores=" + virtualCores + 61 | '}'; 62 | } 63 | } 64 | -------------------------------------------------------------------------------- /twill-yarn/src/main/java/org/apache/twill/internal/appmaster/ApplicationSubmitter.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Licensed to the Apache Software Foundation (ASF) under one 3 | * or more contributor license agreements. See the NOTICE file 4 | * distributed with this work for additional information 5 | * regarding copyright ownership. The ASF licenses this file 6 | * to you under the Apache License, Version 2.0 (the 7 | * "License"); you may not use this file except in compliance 8 | * with the License. You may obtain a copy of the License at 9 | * 10 | * http://www.apache.org/licenses/LICENSE-2.0 11 | * 12 | * Unless required by applicable law or agreed to in writing, software 13 | * distributed under the License is distributed on an "AS IS" BASIS, 14 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 15 | * See the License for the specific language governing permissions and 16 | * limitations under the License. 17 | */ 18 | package org.apache.twill.internal.appmaster; 19 | 20 | import org.apache.twill.internal.ProcessController; 21 | import org.apache.twill.internal.yarn.YarnApplicationReport; 22 | import org.apache.twill.internal.yarn.YarnLaunchContext; 23 | 24 | /** 25 | * Interface for submitting a new application to run. 26 | */ 27 | public interface ApplicationSubmitter { 28 | 29 | ProcessController submit(YarnLaunchContext launchContext); 30 | } 31 | -------------------------------------------------------------------------------- /twill-yarn/src/main/java/org/apache/twill/internal/appmaster/BasicEventHandlerContext.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Licensed to the Apache Software Foundation (ASF) under one 3 | * or more contributor license agreements. See the NOTICE file 4 | * distributed with this work for additional information 5 | * regarding copyright ownership. The ASF licenses this file 6 | * to you under the Apache License, Version 2.0 (the 7 | * "License"); you may not use this file except in compliance 8 | * with the License. You may obtain a copy of the License at 9 | * 10 | * http://www.apache.org/licenses/LICENSE-2.0 11 | * 12 | * Unless required by applicable law or agreed to in writing, software 13 | * distributed under the License is distributed on an "AS IS" BASIS, 14 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 15 | * See the License for the specific language governing permissions and 16 | * limitations under the License. 17 | */ 18 | package org.apache.twill.internal.appmaster; 19 | 20 | import org.apache.twill.api.EventHandlerContext; 21 | import org.apache.twill.api.EventHandlerSpecification; 22 | import org.apache.twill.api.RunId; 23 | import org.apache.twill.internal.TwillRuntimeSpecification; 24 | 25 | /** 26 | * 27 | */ 28 | final class BasicEventHandlerContext implements EventHandlerContext { 29 | 30 | private final String applicationName; 31 | private final RunId runId; 32 | private final EventHandlerSpecification specification; 33 | 34 | public BasicEventHandlerContext(TwillRuntimeSpecification twillRuntimeSpec) { 35 | this.applicationName = twillRuntimeSpec.getTwillAppName(); 36 | this.runId = twillRuntimeSpec.getTwillAppRunId(); 37 | this.specification = twillRuntimeSpec.getTwillSpecification().getEventHandler(); 38 | } 39 | 40 | @Override 41 | public String getApplicationName() { 42 | return applicationName; 43 | } 44 | 45 | @Override 46 | public RunId getRunId() { 47 | return runId; 48 | } 49 | 50 | @Override 51 | public EventHandlerSpecification getSpecification() { 52 | return specification; 53 | } 54 | } 55 | -------------------------------------------------------------------------------- /twill-yarn/src/main/java/org/apache/twill/internal/appmaster/LoggerContextListenerAdapter.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Licensed to the Apache Software Foundation (ASF) under one 3 | * or more contributor license agreements. See the NOTICE file 4 | * distributed with this work for additional information 5 | * regarding copyright ownership. The ASF licenses this file 6 | * to you under the Apache License, Version 2.0 (the 7 | * "License"); you may not use this file except in compliance 8 | * with the License. You may obtain a copy of the License at 9 | * 10 | * http://www.apache.org/licenses/LICENSE-2.0 11 | * 12 | * Unless required by applicable law or agreed to in writing, software 13 | * distributed under the License is distributed on an "AS IS" BASIS, 14 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 15 | * See the License for the specific language governing permissions and 16 | * limitations under the License. 17 | */ 18 | package org.apache.twill.internal.appmaster; 19 | 20 | import ch.qos.logback.classic.Level; 21 | import ch.qos.logback.classic.Logger; 22 | import ch.qos.logback.classic.LoggerContext; 23 | import ch.qos.logback.classic.spi.LoggerContextListener; 24 | 25 | /** 26 | * 27 | */ 28 | abstract class LoggerContextListenerAdapter implements LoggerContextListener { 29 | 30 | private final boolean resetResistant; 31 | 32 | protected LoggerContextListenerAdapter(boolean resetResistant) { 33 | this.resetResistant = resetResistant; 34 | } 35 | 36 | @Override 37 | public final boolean isResetResistant() { 38 | return resetResistant; 39 | } 40 | 41 | @Override 42 | public void onStart(LoggerContext context) { 43 | } 44 | 45 | @Override 46 | public void onReset(LoggerContext context) { 47 | } 48 | 49 | @Override 50 | public void onStop(LoggerContext context) { 51 | } 52 | 53 | @Override 54 | public void onLevelChange(Logger logger, Level level) { 55 | } 56 | } 57 | -------------------------------------------------------------------------------- /twill-yarn/src/main/java/org/apache/twill/internal/appmaster/ProvisionRequest.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Licensed to the Apache Software Foundation (ASF) under one 3 | * or more contributor license agreements. See the NOTICE file 4 | * distributed with this work for additional information 5 | * regarding copyright ownership. The ASF licenses this file 6 | * to you under the Apache License, Version 2.0 (the 7 | * "License"); you may not use this file except in compliance 8 | * with the License. You may obtain a copy of the License at 9 | * 10 | * http://www.apache.org/licenses/LICENSE-2.0 11 | * 12 | * Unless required by applicable law or agreed to in writing, software 13 | * distributed under the License is distributed on an "AS IS" BASIS, 14 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 15 | * See the License for the specific language governing permissions and 16 | * limitations under the License. 17 | */ 18 | package org.apache.twill.internal.appmaster; 19 | 20 | import org.apache.twill.api.RuntimeSpecification; 21 | 22 | /** 23 | * Package private class to help AM to track in progress container request. 24 | */ 25 | final class ProvisionRequest { 26 | private final RuntimeSpecification runtimeSpec; 27 | private final String requestId; 28 | private int requestCount; 29 | private final AllocationSpecification.Type type; 30 | 31 | ProvisionRequest(RuntimeSpecification runtimeSpec, String requestId, int requestCount) { 32 | this(runtimeSpec, requestId, requestCount, AllocationSpecification.Type.DEFAULT); 33 | } 34 | 35 | ProvisionRequest(RuntimeSpecification runtimeSpec, String requestId, int requestCount, 36 | AllocationSpecification.Type type) { 37 | this.runtimeSpec = runtimeSpec; 38 | this.requestId = requestId; 39 | this.requestCount = requestCount; 40 | this.type = type; 41 | } 42 | 43 | RuntimeSpecification getRuntimeSpec() { 44 | return runtimeSpec; 45 | } 46 | 47 | String getRequestId() { 48 | return requestId; 49 | } 50 | 51 | /** 52 | * Called to notify a container has been provision for this request. 53 | * @return {@code true} if the requested container count has been provisioned. 54 | */ 55 | boolean containerAcquired() { 56 | requestCount--; 57 | return requestCount == 0; 58 | } 59 | 60 | AllocationSpecification.Type getType() { 61 | return this.type; 62 | } 63 | } 64 | -------------------------------------------------------------------------------- /twill-yarn/src/main/java/org/apache/twill/internal/appmaster/package-info.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Licensed to the Apache Software Foundation (ASF) under one 3 | * or more contributor license agreements. See the NOTICE file 4 | * distributed with this work for additional information 5 | * regarding copyright ownership. The ASF licenses this file 6 | * to you under the Apache License, Version 2.0 (the 7 | * "License"); you may not use this file except in compliance 8 | * with the License. You may obtain a copy of the License at 9 | * 10 | * http://www.apache.org/licenses/LICENSE-2.0 11 | * 12 | * Unless required by applicable law or agreed to in writing, software 13 | * distributed under the License is distributed on an "AS IS" BASIS, 14 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 15 | * See the License for the specific language governing permissions and 16 | * limitations under the License. 17 | */ 18 | /** 19 | * This package contains implementation of Twill application master. 20 | */ 21 | package org.apache.twill.internal.appmaster; 22 | -------------------------------------------------------------------------------- /twill-yarn/src/main/java/org/apache/twill/internal/yarn/YarnAMClientFactory.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Licensed to the Apache Software Foundation (ASF) under one 3 | * or more contributor license agreements. See the NOTICE file 4 | * distributed with this work for additional information 5 | * regarding copyright ownership. The ASF licenses this file 6 | * to you under the Apache License, Version 2.0 (the 7 | * "License"); you may not use this file except in compliance 8 | * with the License. You may obtain a copy of the License at 9 | * 10 | * http://www.apache.org/licenses/LICENSE-2.0 11 | * 12 | * Unless required by applicable law or agreed to in writing, software 13 | * distributed under the License is distributed on an "AS IS" BASIS, 14 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 15 | * See the License for the specific language governing permissions and 16 | * limitations under the License. 17 | */ 18 | package org.apache.twill.internal.yarn; 19 | 20 | /** 21 | * 22 | */ 23 | public interface YarnAMClientFactory { 24 | 25 | YarnAMClient create(); 26 | } 27 | -------------------------------------------------------------------------------- /twill-yarn/src/main/java/org/apache/twill/internal/yarn/YarnAppClientFactory.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Licensed to the Apache Software Foundation (ASF) under one 3 | * or more contributor license agreements. See the NOTICE file 4 | * distributed with this work for additional information 5 | * regarding copyright ownership. The ASF licenses this file 6 | * to you under the Apache License, Version 2.0 (the 7 | * "License"); you may not use this file except in compliance 8 | * with the License. You may obtain a copy of the License at 9 | * 10 | * http://www.apache.org/licenses/LICENSE-2.0 11 | * 12 | * Unless required by applicable law or agreed to in writing, software 13 | * distributed under the License is distributed on an "AS IS" BASIS, 14 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 15 | * See the License for the specific language governing permissions and 16 | * limitations under the License. 17 | */ 18 | package org.apache.twill.internal.yarn; 19 | 20 | import org.apache.hadoop.conf.Configuration; 21 | 22 | /** 23 | * 24 | */ 25 | public interface YarnAppClientFactory { 26 | 27 | YarnAppClient create(Configuration configuration); 28 | } 29 | -------------------------------------------------------------------------------- /twill-yarn/src/main/java/org/apache/twill/internal/yarn/YarnContainerInfo.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Licensed to the Apache Software Foundation (ASF) under one 3 | * or more contributor license agreements. See the NOTICE file 4 | * distributed with this work for additional information 5 | * regarding copyright ownership. The ASF licenses this file 6 | * to you under the Apache License, Version 2.0 (the 7 | * "License"); you may not use this file except in compliance 8 | * with the License. You may obtain a copy of the License at 9 | * 10 | * http://www.apache.org/licenses/LICENSE-2.0 11 | * 12 | * Unless required by applicable law or agreed to in writing, software 13 | * distributed under the License is distributed on an "AS IS" BASIS, 14 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 15 | * See the License for the specific language governing permissions and 16 | * limitations under the License. 17 | */ 18 | package org.apache.twill.internal.yarn; 19 | 20 | import org.apache.twill.internal.ContainerInfo; 21 | 22 | /** 23 | * 24 | */ 25 | public interface YarnContainerInfo extends ContainerInfo { 26 | 27 | T getContainer(); 28 | } 29 | -------------------------------------------------------------------------------- /twill-yarn/src/main/java/org/apache/twill/internal/yarn/YarnContainerStatus.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Licensed to the Apache Software Foundation (ASF) under one 3 | * or more contributor license agreements. See the NOTICE file 4 | * distributed with this work for additional information 5 | * regarding copyright ownership. The ASF licenses this file 6 | * to you under the Apache License, Version 2.0 (the 7 | * "License"); you may not use this file except in compliance 8 | * with the License. You may obtain a copy of the License at 9 | * 10 | * http://www.apache.org/licenses/LICENSE-2.0 11 | * 12 | * Unless required by applicable law or agreed to in writing, software 13 | * distributed under the License is distributed on an "AS IS" BASIS, 14 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 15 | * See the License for the specific language governing permissions and 16 | * limitations under the License. 17 | */ 18 | package org.apache.twill.internal.yarn; 19 | 20 | import org.apache.hadoop.yarn.api.records.ContainerState; 21 | 22 | /** 23 | * This interface is for adapting differences in ContainerStatus between Hadoop 2.0 and 2.1. 24 | */ 25 | public interface YarnContainerStatus { 26 | 27 | String getContainerId(); 28 | 29 | ContainerState getState(); 30 | 31 | int getExitStatus(); 32 | 33 | String getDiagnostics(); 34 | } 35 | -------------------------------------------------------------------------------- /twill-yarn/src/main/java/org/apache/twill/internal/yarn/YarnLaunchContext.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Licensed to the Apache Software Foundation (ASF) under one 3 | * or more contributor license agreements. See the NOTICE file 4 | * distributed with this work for additional information 5 | * regarding copyright ownership. The ASF licenses this file 6 | * to you under the Apache License, Version 2.0 (the 7 | * "License"); you may not use this file except in compliance 8 | * with the License. You may obtain a copy of the License at 9 | * 10 | * http://www.apache.org/licenses/LICENSE-2.0 11 | * 12 | * Unless required by applicable law or agreed to in writing, software 13 | * distributed under the License is distributed on an "AS IS" BASIS, 14 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 15 | * See the License for the specific language governing permissions and 16 | * limitations under the License. 17 | */ 18 | package org.apache.twill.internal.yarn; 19 | 20 | import org.apache.hadoop.security.Credentials; 21 | import org.apache.hadoop.yarn.api.records.ApplicationAccessType; 22 | 23 | import java.nio.ByteBuffer; 24 | import java.util.List; 25 | import java.util.Map; 26 | 27 | /** 28 | * This interface is for adapting ContainerLaunchContext in different Hadoop version. 29 | */ 30 | public interface YarnLaunchContext { 31 | 32 | T getLaunchContext(); 33 | 34 | void setCredentials(Credentials credentials); 35 | 36 | void setLocalResources(Map localResources); 37 | 38 | void setServiceData(Map serviceData); 39 | 40 | Map getEnvironment(); 41 | 42 | void setEnvironment(Map environment); 43 | 44 | List getCommands(); 45 | 46 | void setCommands(List commands); 47 | 48 | void setApplicationACLs(Map acls); 49 | } 50 | -------------------------------------------------------------------------------- /twill-yarn/src/main/java/org/apache/twill/internal/yarn/YarnNMClient.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Licensed to the Apache Software Foundation (ASF) under one 3 | * or more contributor license agreements. See the NOTICE file 4 | * distributed with this work for additional information 5 | * regarding copyright ownership. The ASF licenses this file 6 | * to you under the Apache License, Version 2.0 (the 7 | * "License"); you may not use this file except in compliance 8 | * with the License. You may obtain a copy of the License at 9 | * 10 | * http://www.apache.org/licenses/LICENSE-2.0 11 | * 12 | * Unless required by applicable law or agreed to in writing, software 13 | * distributed under the License is distributed on an "AS IS" BASIS, 14 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 15 | * See the License for the specific language governing permissions and 16 | * limitations under the License. 17 | */ 18 | package org.apache.twill.internal.yarn; 19 | 20 | import org.apache.twill.common.Cancellable; 21 | 22 | /** 23 | * Abstraction for dealing with API differences in different hadoop yarn version. 24 | */ 25 | public interface YarnNMClient { 26 | 27 | /** 28 | * Starts a process based on the given launch context. 29 | * 30 | * @param containerInfo The containerInfo that the new process will launch in. 31 | * @param launchContext Contains information about the process going to start. 32 | * @return A {@link Cancellable} that when {@link Cancellable#cancel()}} is invoked, 33 | * it will try to shutdown the process. 34 | * 35 | */ 36 | Cancellable start(YarnContainerInfo containerInfo, YarnLaunchContext launchContext); 37 | } 38 | -------------------------------------------------------------------------------- /twill-yarn/src/main/java/org/apache/twill/internal/yarn/package-info.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Licensed to the Apache Software Foundation (ASF) under one 3 | * or more contributor license agreements. See the NOTICE file 4 | * distributed with this work for additional information 5 | * regarding copyright ownership. The ASF licenses this file 6 | * to you under the Apache License, Version 2.0 (the 7 | * "License"); you may not use this file except in compliance 8 | * with the License. You may obtain a copy of the License at 9 | * 10 | * http://www.apache.org/licenses/LICENSE-2.0 11 | * 12 | * Unless required by applicable law or agreed to in writing, software 13 | * distributed under the License is distributed on an "AS IS" BASIS, 14 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 15 | * See the License for the specific language governing permissions and 16 | * limitations under the License. 17 | */ 18 | /** 19 | * This package contains class for interacting with Yarn. 20 | */ 21 | package org.apache.twill.internal.yarn; 22 | -------------------------------------------------------------------------------- /twill-yarn/src/main/java/org/apache/twill/yarn/LocationSecureStoreRenewer.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Licensed to the Apache Software Foundation (ASF) under one 3 | * or more contributor license agreements. See the NOTICE file 4 | * distributed with this work for additional information 5 | * regarding copyright ownership. The ASF licenses this file 6 | * to you under the Apache License, Version 2.0 (the 7 | * "License"); you may not use this file except in compliance 8 | * with the License. You may obtain a copy of the License at 9 | * 10 | * http://www.apache.org/licenses/LICENSE-2.0 11 | * 12 | * Unless required by applicable law or agreed to in writing, software 13 | * distributed under the License is distributed on an "AS IS" BASIS, 14 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 15 | * See the License for the specific language governing permissions and 16 | * limitations under the License. 17 | */ 18 | package org.apache.twill.yarn; 19 | 20 | import org.apache.hadoop.conf.Configuration; 21 | import org.apache.hadoop.security.Credentials; 22 | import org.apache.twill.api.RunId; 23 | import org.apache.twill.api.security.SecureStoreRenewer; 24 | import org.apache.twill.api.security.SecureStoreWriter; 25 | import org.apache.twill.filesystem.LocationFactory; 26 | import org.apache.twill.internal.yarn.YarnUtils; 27 | 28 | import java.io.IOException; 29 | 30 | /** 31 | * Package private class for updating location related secure store. 32 | */ 33 | final class LocationSecureStoreRenewer extends SecureStoreRenewer { 34 | 35 | private final Configuration configuration; 36 | private final LocationFactory locationFactory; 37 | 38 | LocationSecureStoreRenewer(Configuration configuration, LocationFactory locationFactory) { 39 | this.configuration = configuration; 40 | this.locationFactory = locationFactory; 41 | } 42 | 43 | @Override 44 | public void renew(String application, RunId runId, SecureStoreWriter secureStoreWriter) throws IOException { 45 | Credentials credentials = new Credentials(); 46 | YarnUtils.addDelegationTokens(configuration, locationFactory, credentials); 47 | secureStoreWriter.write(YarnSecureStore.create(credentials)); 48 | } 49 | } 50 | -------------------------------------------------------------------------------- /twill-yarn/src/main/java/org/apache/twill/yarn/YarnSecureStore.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Licensed to the Apache Software Foundation (ASF) under one 3 | * or more contributor license agreements. See the NOTICE file 4 | * distributed with this work for additional information 5 | * regarding copyright ownership. The ASF licenses this file 6 | * to you under the Apache License, Version 2.0 (the 7 | * "License"); you may not use this file except in compliance 8 | * with the License. You may obtain a copy of the License at 9 | * 10 | * http://www.apache.org/licenses/LICENSE-2.0 11 | * 12 | * Unless required by applicable law or agreed to in writing, software 13 | * distributed under the License is distributed on an "AS IS" BASIS, 14 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 15 | * See the License for the specific language governing permissions and 16 | * limitations under the License. 17 | */ 18 | package org.apache.twill.yarn; 19 | 20 | import org.apache.hadoop.security.Credentials; 21 | import org.apache.twill.api.SecureStore; 22 | 23 | /** 24 | * A {@link SecureStore} for hadoop credentials. 25 | */ 26 | public final class YarnSecureStore implements SecureStore { 27 | 28 | private final Credentials credentials; 29 | 30 | public static SecureStore create(Credentials credentials) { 31 | return new YarnSecureStore(credentials); 32 | } 33 | 34 | private YarnSecureStore(Credentials credentials) { 35 | this.credentials = credentials; 36 | } 37 | 38 | @Override 39 | public Credentials getStore() { 40 | return credentials; 41 | } 42 | } 43 | -------------------------------------------------------------------------------- /twill-yarn/src/main/java/org/apache/twill/yarn/YarnTwillControllerFactory.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Licensed to the Apache Software Foundation (ASF) under one 3 | * or more contributor license agreements. See the NOTICE file 4 | * distributed with this work for additional information 5 | * regarding copyright ownership. The ASF licenses this file 6 | * to you under the Apache License, Version 2.0 (the 7 | * "License"); you may not use this file except in compliance 8 | * with the License. You may obtain a copy of the License at 9 | * 10 | * http://www.apache.org/licenses/LICENSE-2.0 11 | * 12 | * Unless required by applicable law or agreed to in writing, software 13 | * distributed under the License is distributed on an "AS IS" BASIS, 14 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 15 | * See the License for the specific language governing permissions and 16 | * limitations under the License. 17 | */ 18 | package org.apache.twill.yarn; 19 | 20 | import org.apache.twill.api.RunId; 21 | import org.apache.twill.api.logging.LogHandler; 22 | import org.apache.twill.internal.ProcessController; 23 | import org.apache.twill.internal.yarn.YarnApplicationReport; 24 | 25 | import java.util.concurrent.Callable; 26 | import java.util.concurrent.TimeUnit; 27 | 28 | /** 29 | * Factory for creating {@link YarnTwillController}. 30 | */ 31 | interface YarnTwillControllerFactory { 32 | 33 | YarnTwillController create(RunId runId, boolean logCollectionEnabled, Iterable logHandlers, 34 | Callable> startUp, 35 | long startTimeout, TimeUnit startTimeoutUnit); 36 | } 37 | -------------------------------------------------------------------------------- /twill-yarn/src/main/java/org/apache/twill/yarn/package-info.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Licensed to the Apache Software Foundation (ASF) under one 3 | * or more contributor license agreements. See the NOTICE file 4 | * distributed with this work for additional information 5 | * regarding copyright ownership. The ASF licenses this file 6 | * to you under the Apache License, Version 2.0 (the 7 | * "License"); you may not use this file except in compliance 8 | * with the License. You may obtain a copy of the License at 9 | * 10 | * http://www.apache.org/licenses/LICENSE-2.0 11 | * 12 | * Unless required by applicable law or agreed to in writing, software 13 | * distributed under the License is distributed on an "AS IS" BASIS, 14 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 15 | * See the License for the specific language governing permissions and 16 | * limitations under the License. 17 | */ 18 | /** 19 | * Classes in this package implement the Twill API for Apache Hadoop YARN. 20 | */ 21 | package org.apache.twill.yarn; 22 | -------------------------------------------------------------------------------- /twill-yarn/src/main/resources/logback-template.xml: -------------------------------------------------------------------------------- 1 | 2 | 18 | 19 | 20 | 21 | 22 | 23 | 24 | 25 | 26 | 27 | 28 | -------------------------------------------------------------------------------- /twill-yarn/src/test/java/org/apache/twill/filesystem/LocalLocationTest.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Licensed to the Apache Software Foundation (ASF) under one 3 | * or more contributor license agreements. See the NOTICE file 4 | * distributed with this work for additional information 5 | * regarding copyright ownership. The ASF licenses this file 6 | * to you under the Apache License, Version 2.0 (the 7 | * "License"); you may not use this file except in compliance 8 | * with the License. You may obtain a copy of the License at 9 | * 10 | * http://www.apache.org/licenses/LICENSE-2.0 11 | * 12 | * Unless required by applicable law or agreed to in writing, software 13 | * distributed under the License is distributed on an "AS IS" BASIS, 14 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 15 | * See the License for the specific language governing permissions and 16 | * limitations under the License. 17 | */ 18 | package org.apache.twill.filesystem; 19 | 20 | import org.apache.hadoop.security.UserGroupInformation; 21 | 22 | import java.io.File; 23 | import java.io.IOException; 24 | 25 | /** 26 | * 27 | */ 28 | public class LocalLocationTest extends LocationTestBase { 29 | 30 | @Override 31 | protected LocationFactory createLocationFactory(String pathBase) throws Exception { 32 | File basePath = new File(tmpFolder.newFolder(), pathBase); 33 | //noinspection ResultOfMethodCallIgnored 34 | basePath.mkdirs(); 35 | return new LocalLocationFactory(basePath); 36 | } 37 | 38 | @Override 39 | protected UserGroupInformation createTestUGI() throws IOException { 40 | // In local location, UGI is not supported, hence using the current user as the testing ugi. 41 | return UserGroupInformation.getCurrentUser(); 42 | } 43 | } 44 | -------------------------------------------------------------------------------- /twill-yarn/src/test/java/org/apache/twill/yarn/BuggyServer.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Licensed to the Apache Software Foundation (ASF) under one 3 | * or more contributor license agreements. See the NOTICE file 4 | * distributed with this work for additional information 5 | * regarding copyright ownership. The ASF licenses this file 6 | * to you under the Apache License, Version 2.0 (the 7 | * "License"); you may not use this file except in compliance 8 | * with the License. You may obtain a copy of the License at 9 | * 10 | * http://www.apache.org/licenses/LICENSE-2.0 11 | * 12 | * Unless required by applicable law or agreed to in writing, software 13 | * distributed under the License is distributed on an "AS IS" BASIS, 14 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 15 | * See the License for the specific language governing permissions and 16 | * limitations under the License. 17 | */ 18 | package org.apache.twill.yarn; 19 | 20 | import org.slf4j.Logger; 21 | import org.slf4j.LoggerFactory; 22 | 23 | import java.io.BufferedReader; 24 | import java.io.IOException; 25 | import java.io.PrintWriter; 26 | 27 | /** 28 | * Server for testing that will die if you give it a 0. 29 | */ 30 | public final class BuggyServer extends SocketServer { 31 | 32 | private static final Logger LOG = LoggerFactory.getLogger(BuggyServer.class); 33 | 34 | @Override 35 | public void handleRequest(BufferedReader reader, PrintWriter writer) throws IOException { 36 | String line = reader.readLine(); 37 | LOG.info("Received: " + line + " going to divide by it"); 38 | Integer toDivide = Integer.valueOf(line); 39 | writer.println(Integer.toString(100 / toDivide)); 40 | } 41 | } 42 | -------------------------------------------------------------------------------- /twill-yarn/src/test/java/org/apache/twill/yarn/CustomClassLoaderRunnable.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Licensed to the Apache Software Foundation (ASF) under one 3 | * or more contributor license agreements. See the NOTICE file 4 | * distributed with this work for additional information 5 | * regarding copyright ownership. The ASF licenses this file 6 | * to you under the Apache License, Version 2.0 (the 7 | * "License"); you may not use this file except in compliance 8 | * with the License. You may obtain a copy of the License at 9 | * 10 | * http://www.apache.org/licenses/LICENSE-2.0 11 | * 12 | * Unless required by applicable law or agreed to in writing, software 13 | * distributed under the License is distributed on an "AS IS" BASIS, 14 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 15 | * See the License for the specific language governing permissions and 16 | * limitations under the License. 17 | */ 18 | package org.apache.twill.yarn; 19 | 20 | import com.google.common.util.concurrent.Uninterruptibles; 21 | import org.apache.twill.api.AbstractTwillRunnable; 22 | import org.apache.twill.api.ServiceAnnouncer; 23 | import org.slf4j.Logger; 24 | import org.slf4j.LoggerFactory; 25 | 26 | import java.util.concurrent.CountDownLatch; 27 | 28 | /** 29 | * Runnable for testing custom classloader 30 | */ 31 | public final class CustomClassLoaderRunnable extends AbstractTwillRunnable { 32 | 33 | static final String GENERATED_CLASS_NAME = "org.apache.twill.test.Generated"; 34 | 35 | private static final Logger LOG = LoggerFactory.getLogger(CustomClassLoaderRunnable.class); 36 | 37 | private final CountDownLatch stopLatch = new CountDownLatch(1); 38 | 39 | @Override 40 | public void run() { 41 | try { 42 | Class cls = Class.forName(GENERATED_CLASS_NAME); 43 | java.lang.reflect.Method announce = cls.getMethod("announce", ServiceAnnouncer.class, String.class, int.class); 44 | announce.invoke(cls.newInstance(), getContext(), System.getProperty("service.name"), 45 | Integer.getInteger("service.port")); 46 | Uninterruptibles.awaitUninterruptibly(stopLatch); 47 | } catch (Exception e) { 48 | LOG.error("Failed to call announce on " + GENERATED_CLASS_NAME, e); 49 | } 50 | } 51 | 52 | @Override 53 | public void stop() { 54 | stopLatch.countDown(); 55 | } 56 | } 57 | -------------------------------------------------------------------------------- /twill-yarn/src/test/java/org/apache/twill/yarn/CustomClassLoaderTestRun.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Licensed to the Apache Software Foundation (ASF) under one 3 | * or more contributor license agreements. See the NOTICE file 4 | * distributed with this work for additional information 5 | * regarding copyright ownership. The ASF licenses this file 6 | * to you under the Apache License, Version 2.0 (the 7 | * "License"); you may not use this file except in compliance 8 | * with the License. You may obtain a copy of the License at 9 | * 10 | * http://www.apache.org/licenses/LICENSE-2.0 11 | * 12 | * Unless required by applicable law or agreed to in writing, software 13 | * distributed under the License is distributed on an "AS IS" BASIS, 14 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 15 | * See the License for the specific language governing permissions and 16 | * limitations under the License. 17 | */ 18 | package org.apache.twill.yarn; 19 | 20 | import org.apache.twill.api.TwillController; 21 | import org.apache.twill.api.logging.PrinterLogHandler; 22 | import org.junit.Assert; 23 | import org.junit.Test; 24 | 25 | import java.io.PrintWriter; 26 | 27 | /** 28 | * Unit test for testing custom classloader for containers. 29 | */ 30 | public class CustomClassLoaderTestRun extends BaseYarnTest { 31 | 32 | @Test 33 | public void testCustomClassLoader() throws Exception { 34 | TwillController controller = getTwillRunner().prepare(new CustomClassLoaderRunnable()) 35 | .setClassLoader(CustomClassLoader.class.getName()) 36 | .addLogHandler(new PrinterLogHandler(new PrintWriter(System.out, true))) 37 | .setJVMOptions("-Dservice.port=54321") 38 | .setJVMOptions(CustomClassLoaderRunnable.class.getSimpleName(), "-Dservice.name=custom") 39 | .start(); 40 | 41 | Assert.assertTrue(waitForSize(controller.discoverService("custom"), 1, 120)); 42 | controller.terminate().get(); 43 | } 44 | } 45 | -------------------------------------------------------------------------------- /twill-yarn/src/test/java/org/apache/twill/yarn/DistributeShellTestRun.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Licensed to the Apache Software Foundation (ASF) under one 3 | * or more contributor license agreements. See the NOTICE file 4 | * distributed with this work for additional information 5 | * regarding copyright ownership. The ASF licenses this file 6 | * to you under the Apache License, Version 2.0 (the 7 | * "License"); you may not use this file except in compliance 8 | * with the License. You may obtain a copy of the License at 9 | * 10 | * http://www.apache.org/licenses/LICENSE-2.0 11 | * 12 | * Unless required by applicable law or agreed to in writing, software 13 | * distributed under the License is distributed on an "AS IS" BASIS, 14 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 15 | * See the License for the specific language governing permissions and 16 | * limitations under the License. 17 | */ 18 | package org.apache.twill.yarn; 19 | 20 | import org.apache.twill.api.TwillController; 21 | import org.apache.twill.api.TwillRunner; 22 | import org.apache.twill.api.logging.PrinterLogHandler; 23 | import org.apache.twill.common.Threads; 24 | import org.junit.Assert; 25 | import org.junit.Ignore; 26 | import org.junit.Test; 27 | 28 | import java.io.PrintWriter; 29 | import java.util.concurrent.CountDownLatch; 30 | import java.util.concurrent.TimeUnit; 31 | 32 | /** 33 | * This test is executed by {@link YarnTestSuite}. 34 | */ 35 | public final class DistributeShellTestRun extends BaseYarnTest { 36 | 37 | @Ignore 38 | @Test 39 | public void testDistributedShell() throws InterruptedException { 40 | TwillRunner twillRunner = getTwillRunner(); 41 | 42 | TwillController controller = twillRunner.prepare(new DistributedShell("pwd", "ls -al")) 43 | .addLogHandler(new PrinterLogHandler(new PrintWriter(System.out))) 44 | .start(); 45 | 46 | final CountDownLatch stopLatch = new CountDownLatch(1); 47 | controller.onTerminated(new Runnable() { 48 | @Override 49 | public void run() { 50 | stopLatch.countDown(); 51 | } 52 | }, Threads.SAME_THREAD_EXECUTOR); 53 | 54 | Assert.assertTrue(stopLatch.await(10, TimeUnit.SECONDS)); 55 | } 56 | } 57 | -------------------------------------------------------------------------------- /twill-yarn/src/test/java/org/apache/twill/yarn/EchoServer.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Licensed to the Apache Software Foundation (ASF) under one 3 | * or more contributor license agreements. See the NOTICE file 4 | * distributed with this work for additional information 5 | * regarding copyright ownership. The ASF licenses this file 6 | * to you under the Apache License, Version 2.0 (the 7 | * "License"); you may not use this file except in compliance 8 | * with the License. You may obtain a copy of the License at 9 | * 10 | * http://www.apache.org/licenses/LICENSE-2.0 11 | * 12 | * Unless required by applicable law or agreed to in writing, software 13 | * distributed under the License is distributed on an "AS IS" BASIS, 14 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 15 | * See the License for the specific language governing permissions and 16 | * limitations under the License. 17 | */ 18 | package org.apache.twill.yarn; 19 | 20 | import org.apache.twill.api.Command; 21 | import org.slf4j.Logger; 22 | import org.slf4j.LoggerFactory; 23 | 24 | import java.io.BufferedReader; 25 | import java.io.IOException; 26 | import java.io.PrintWriter; 27 | 28 | /** 29 | * Test server that echoes back what it receives. 30 | */ 31 | public final class EchoServer extends SocketServer { 32 | 33 | private static final Logger LOG = LoggerFactory.getLogger(EchoServer.class); 34 | 35 | @Override 36 | public void handleRequest(BufferedReader reader, PrintWriter writer) throws IOException { 37 | String line = reader.readLine(); 38 | LOG.info("Received: " + line); 39 | if (line != null) { 40 | writer.println(line); 41 | } 42 | } 43 | 44 | @Override 45 | public void handleCommand(Command command) throws Exception { 46 | LOG.info("Command received: " + command + " " + getContext().getInstanceCount()); 47 | } 48 | } 49 | -------------------------------------------------------------------------------- /twill-yarn/src/test/java/org/apache/twill/yarn/EnvironmentEchoServer.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Licensed to the Apache Software Foundation (ASF) under one 3 | * or more contributor license agreements. See the NOTICE file 4 | * distributed with this work for additional information 5 | * regarding copyright ownership. The ASF licenses this file 6 | * to you under the Apache License, Version 2.0 (the 7 | * "License"); you may not use this file except in compliance 8 | * with the License. You may obtain a copy of the License at 9 | * 10 | * http://www.apache.org/licenses/LICENSE-2.0 11 | * 12 | * Unless required by applicable law or agreed to in writing, software 13 | * distributed under the License is distributed on an "AS IS" BASIS, 14 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 15 | * See the License for the specific language governing permissions and 16 | * limitations under the License. 17 | */ 18 | package org.apache.twill.yarn; 19 | 20 | import java.io.BufferedReader; 21 | import java.io.IOException; 22 | import java.io.PrintWriter; 23 | 24 | /** 25 | * Test server that returns back the value of the env key sent in. Used to check env for 26 | * runnables is correctly set. 27 | */ 28 | public class EnvironmentEchoServer extends SocketServer { 29 | 30 | @Override 31 | public void handleRequest(BufferedReader reader, PrintWriter writer) throws IOException { 32 | String envKey = reader.readLine(); 33 | writer.println(System.getenv(envKey)); 34 | } 35 | } 36 | -------------------------------------------------------------------------------- /twill-yarn/src/test/java/org/apache/twill/yarn/YarnTestSuite.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Licensed to the Apache Software Foundation (ASF) under one 3 | * or more contributor license agreements. See the NOTICE file 4 | * distributed with this work for additional information 5 | * regarding copyright ownership. The ASF licenses this file 6 | * to you under the Apache License, Version 2.0 (the 7 | * "License"); you may not use this file except in compliance 8 | * with the License. You may obtain a copy of the License at 9 | * 10 | * http://www.apache.org/licenses/LICENSE-2.0 11 | * 12 | * Unless required by applicable law or agreed to in writing, software 13 | * distributed under the License is distributed on an "AS IS" BASIS, 14 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 15 | * See the License for the specific language governing permissions and 16 | * limitations under the License. 17 | */ 18 | package org.apache.twill.yarn; 19 | 20 | import org.junit.runner.RunWith; 21 | import org.junit.runners.Suite; 22 | 23 | /** 24 | * Test suite for all tests with mini yarn cluster. 25 | */ 26 | @RunWith(Suite.class) 27 | @Suite.SuiteClasses({ 28 | ContainerSizeTestRun.class, 29 | CustomClassLoaderTestRun.class, 30 | DebugTestRun.class, 31 | DistributeShellTestRun.class, 32 | EchoServerTestRun.class, 33 | EnvironmentTestRun.class, 34 | EventHandlerTestRun.class, 35 | FailureRestartTestRun.class, 36 | InitializeFailTestRun.class, 37 | JvmOptionsTestRun.class, 38 | LocalFileTestRun.class, 39 | LogHandlerTestRun.class, 40 | LogLevelChangeTestRun.class, 41 | LogLevelTestRun.class, 42 | PlacementPolicyTestRun.class, 43 | ProvisionTimeoutTestRun.class, 44 | ResourceReportTestRun.class, 45 | ServiceDiscoveryTestRun.class, 46 | SessionExpireTestRun.class, 47 | TaskCompletedTestRun.class, 48 | RestartRunnableTestRun.class, 49 | MaxRetriesTestRun.class 50 | }) 51 | public final class YarnTestSuite extends BaseYarnTest { 52 | } 53 | -------------------------------------------------------------------------------- /twill-yarn/src/test/resources/logback-test.xml: -------------------------------------------------------------------------------- 1 | 2 | 18 | 19 | 20 | 21 | 22 | 23 | %d{ISO8601} - %-5p [%t:%C{1}@%L] - %m%n 24 | 25 | 26 | 27 | 28 | 29 | 30 | 31 | 32 | 33 | 34 | 35 | -------------------------------------------------------------------------------- /twill-zookeeper/src/main/java/org/apache/twill/internal/zookeeper/BasicACLData.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Licensed to the Apache Software Foundation (ASF) under one 3 | * or more contributor license agreements. See the NOTICE file 4 | * distributed with this work for additional information 5 | * regarding copyright ownership. The ASF licenses this file 6 | * to you under the Apache License, Version 2.0 (the 7 | * "License"); you may not use this file except in compliance 8 | * with the License. You may obtain a copy of the License at 9 | * 10 | * http://www.apache.org/licenses/LICENSE-2.0 11 | * 12 | * Unless required by applicable law or agreed to in writing, software 13 | * distributed under the License is distributed on an "AS IS" BASIS, 14 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 15 | * See the License for the specific language governing permissions and 16 | * limitations under the License. 17 | */ 18 | package org.apache.twill.internal.zookeeper; 19 | 20 | import org.apache.twill.zookeeper.ACLData; 21 | import org.apache.zookeeper.data.ACL; 22 | import org.apache.zookeeper.data.Stat; 23 | 24 | import java.util.List; 25 | 26 | /** 27 | * A straightforward implementation of {@link ACLData}. 28 | */ 29 | final class BasicACLData implements ACLData { 30 | 31 | private final List acl; 32 | private final Stat stat; 33 | 34 | BasicACLData(List acl, Stat stat) { 35 | this.acl = acl; 36 | this.stat = stat; 37 | } 38 | 39 | @Override 40 | public List getACL() { 41 | return acl; 42 | } 43 | 44 | @Override 45 | public Stat getStat() { 46 | return stat; 47 | } 48 | } 49 | -------------------------------------------------------------------------------- /twill-zookeeper/src/main/java/org/apache/twill/internal/zookeeper/BasicNodeChildren.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Licensed to the Apache Software Foundation (ASF) under one 3 | * or more contributor license agreements. See the NOTICE file 4 | * distributed with this work for additional information 5 | * regarding copyright ownership. The ASF licenses this file 6 | * to you under the Apache License, Version 2.0 (the 7 | * "License"); you may not use this file except in compliance 8 | * with the License. You may obtain a copy of the License at 9 | * 10 | * http://www.apache.org/licenses/LICENSE-2.0 11 | * 12 | * Unless required by applicable law or agreed to in writing, software 13 | * distributed under the License is distributed on an "AS IS" BASIS, 14 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 15 | * See the License for the specific language governing permissions and 16 | * limitations under the License. 17 | */ 18 | package org.apache.twill.internal.zookeeper; 19 | 20 | import com.google.common.base.Objects; 21 | import org.apache.twill.zookeeper.NodeChildren; 22 | import org.apache.zookeeper.data.Stat; 23 | 24 | import java.util.List; 25 | 26 | /** 27 | * 28 | */ 29 | final class BasicNodeChildren implements NodeChildren { 30 | 31 | private final Stat stat; 32 | private final List children; 33 | 34 | BasicNodeChildren(List children, Stat stat) { 35 | this.stat = stat; 36 | this.children = children; 37 | } 38 | 39 | @Override 40 | public Stat getStat() { 41 | return stat; 42 | } 43 | 44 | @Override 45 | public List getChildren() { 46 | return children; 47 | } 48 | 49 | @Override 50 | public boolean equals(Object o) { 51 | if (this == o) { 52 | return true; 53 | } 54 | if (o == null || !(o instanceof NodeChildren)) { 55 | return false; 56 | } 57 | 58 | NodeChildren that = (NodeChildren) o; 59 | return stat.equals(that.getStat()) && children.equals(that.getChildren()); 60 | } 61 | 62 | @Override 63 | public int hashCode() { 64 | return Objects.hashCode(children, stat); 65 | } 66 | } 67 | -------------------------------------------------------------------------------- /twill-zookeeper/src/main/java/org/apache/twill/internal/zookeeper/BasicNodeData.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Licensed to the Apache Software Foundation (ASF) under one 3 | * or more contributor license agreements. See the NOTICE file 4 | * distributed with this work for additional information 5 | * regarding copyright ownership. The ASF licenses this file 6 | * to you under the Apache License, Version 2.0 (the 7 | * "License"); you may not use this file except in compliance 8 | * with the License. You may obtain a copy of the License at 9 | * 10 | * http://www.apache.org/licenses/LICENSE-2.0 11 | * 12 | * Unless required by applicable law or agreed to in writing, software 13 | * distributed under the License is distributed on an "AS IS" BASIS, 14 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 15 | * See the License for the specific language governing permissions and 16 | * limitations under the License. 17 | */ 18 | package org.apache.twill.internal.zookeeper; 19 | 20 | import com.google.common.base.Objects; 21 | import org.apache.twill.zookeeper.NodeData; 22 | import org.apache.zookeeper.data.Stat; 23 | 24 | import java.util.Arrays; 25 | 26 | /** 27 | * A straightforward implementation for {@link NodeData}. 28 | */ 29 | final class BasicNodeData implements NodeData { 30 | 31 | private final byte[] data; 32 | private final Stat stat; 33 | 34 | BasicNodeData(byte[] data, Stat stat) { 35 | this.data = data; 36 | this.stat = stat; 37 | } 38 | 39 | @Override 40 | public Stat getStat() { 41 | return stat; 42 | } 43 | 44 | @Override 45 | public byte[] getData() { 46 | return data; 47 | } 48 | 49 | @Override 50 | public boolean equals(Object o) { 51 | if (this == o) { 52 | return true; 53 | } 54 | if (o == null || !(o instanceof NodeData)) { 55 | return false; 56 | } 57 | 58 | BasicNodeData that = (BasicNodeData) o; 59 | 60 | return stat.equals(that.getStat()) && Arrays.equals(data, that.getData()); 61 | } 62 | 63 | @Override 64 | public int hashCode() { 65 | return Objects.hashCode(data, stat); 66 | } 67 | } 68 | -------------------------------------------------------------------------------- /twill-zookeeper/src/main/java/org/apache/twill/internal/zookeeper/RetryUtils.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Licensed to the Apache Software Foundation (ASF) under one 3 | * or more contributor license agreements. See the NOTICE file 4 | * distributed with this work for additional information 5 | * regarding copyright ownership. The ASF licenses this file 6 | * to you under the Apache License, Version 2.0 (the 7 | * "License"); you may not use this file except in compliance 8 | * with the License. You may obtain a copy of the License at 9 | * 10 | * http://www.apache.org/licenses/LICENSE-2.0 11 | * 12 | * Unless required by applicable law or agreed to in writing, software 13 | * distributed under the License is distributed on an "AS IS" BASIS, 14 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 15 | * See the License for the specific language governing permissions and 16 | * limitations under the License. 17 | */ 18 | package org.apache.twill.internal.zookeeper; 19 | 20 | import org.apache.zookeeper.KeeperException; 21 | 22 | /** 23 | * Utility class for help determining operation retry condition. 24 | */ 25 | final class RetryUtils { 26 | 27 | /** 28 | * Tells if a given operation error code can be retried or not. 29 | * @param code The error code of the operation. 30 | * @return {@code true} if the operation can be retried. 31 | */ 32 | public static boolean canRetry(KeeperException.Code code) { 33 | return (code == KeeperException.Code.CONNECTIONLOSS 34 | || code == KeeperException.Code.OPERATIONTIMEOUT 35 | || code == KeeperException.Code.SESSIONEXPIRED 36 | || code == KeeperException.Code.SESSIONMOVED); 37 | } 38 | 39 | /** 40 | * Tells if a given operation exception can be retried or not. 41 | * @param t The exception raised by an operation. 42 | * @return {@code true} if the operation can be retried. 43 | */ 44 | public static boolean canRetry(Throwable t) { 45 | return t instanceof KeeperException && canRetry(((KeeperException) t).code()); 46 | } 47 | 48 | private RetryUtils() { 49 | } 50 | } 51 | -------------------------------------------------------------------------------- /twill-zookeeper/src/main/java/org/apache/twill/internal/zookeeper/package-info.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Licensed to the Apache Software Foundation (ASF) under one 3 | * or more contributor license agreements. See the NOTICE file 4 | * distributed with this work for additional information 5 | * regarding copyright ownership. The ASF licenses this file 6 | * to you under the Apache License, Version 2.0 (the 7 | * "License"); you may not use this file except in compliance 8 | * with the License. You may obtain a copy of the License at 9 | * 10 | * http://www.apache.org/licenses/LICENSE-2.0 11 | * 12 | * Unless required by applicable law or agreed to in writing, software 13 | * distributed under the License is distributed on an "AS IS" BASIS, 14 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 15 | * See the License for the specific language governing permissions and 16 | * limitations under the License. 17 | */ 18 | 19 | /** 20 | * Internal classes for zookeeper. 21 | */ 22 | package org.apache.twill.internal.zookeeper; 23 | -------------------------------------------------------------------------------- /twill-zookeeper/src/main/java/org/apache/twill/zookeeper/ACLData.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Licensed to the Apache Software Foundation (ASF) under one 3 | * or more contributor license agreements. See the NOTICE file 4 | * distributed with this work for additional information 5 | * regarding copyright ownership. The ASF licenses this file 6 | * to you under the Apache License, Version 2.0 (the 7 | * "License"); you may not use this file except in compliance 8 | * with the License. You may obtain a copy of the License at 9 | * 10 | * http://www.apache.org/licenses/LICENSE-2.0 11 | * 12 | * Unless required by applicable law or agreed to in writing, software 13 | * distributed under the License is distributed on an "AS IS" BASIS, 14 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 15 | * See the License for the specific language governing permissions and 16 | * limitations under the License. 17 | */ 18 | package org.apache.twill.zookeeper; 19 | 20 | import org.apache.zookeeper.data.ACL; 21 | import org.apache.zookeeper.data.Stat; 22 | 23 | import java.util.List; 24 | 25 | /** 26 | * Represents result of call to {@link ZKClient#getACL(String)}. 27 | */ 28 | public interface ACLData { 29 | 30 | /** 31 | * @return list of {@link ACL} for the node. 32 | */ 33 | List getACL(); 34 | 35 | /** 36 | * @return The {@link Stat} of the node. 37 | */ 38 | Stat getStat(); 39 | } 40 | -------------------------------------------------------------------------------- /twill-zookeeper/src/main/java/org/apache/twill/zookeeper/NodeChildren.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Licensed to the Apache Software Foundation (ASF) under one 3 | * or more contributor license agreements. See the NOTICE file 4 | * distributed with this work for additional information 5 | * regarding copyright ownership. The ASF licenses this file 6 | * to you under the Apache License, Version 2.0 (the 7 | * "License"); you may not use this file except in compliance 8 | * with the License. You may obtain a copy of the License at 9 | * 10 | * http://www.apache.org/licenses/LICENSE-2.0 11 | * 12 | * Unless required by applicable law or agreed to in writing, software 13 | * distributed under the License is distributed on an "AS IS" BASIS, 14 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 15 | * See the License for the specific language governing permissions and 16 | * limitations under the License. 17 | */ 18 | package org.apache.twill.zookeeper; 19 | 20 | import org.apache.zookeeper.data.Stat; 21 | 22 | import java.util.List; 23 | 24 | /** 25 | * Represents result of call to {@link ZKClientService#getChildren(String, org.apache.zookeeper.Watcher)} method. 26 | */ 27 | public interface NodeChildren { 28 | 29 | /** 30 | * @return The {@link Stat} of the node. 31 | */ 32 | Stat getStat(); 33 | 34 | /** 35 | * @return List of children node names. 36 | */ 37 | List getChildren(); 38 | } 39 | -------------------------------------------------------------------------------- /twill-zookeeper/src/main/java/org/apache/twill/zookeeper/NodeData.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Licensed to the Apache Software Foundation (ASF) under one 3 | * or more contributor license agreements. See the NOTICE file 4 | * distributed with this work for additional information 5 | * regarding copyright ownership. The ASF licenses this file 6 | * to you under the Apache License, Version 2.0 (the 7 | * "License"); you may not use this file except in compliance 8 | * with the License. You may obtain a copy of the License at 9 | * 10 | * http://www.apache.org/licenses/LICENSE-2.0 11 | * 12 | * Unless required by applicable law or agreed to in writing, software 13 | * distributed under the License is distributed on an "AS IS" BASIS, 14 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 15 | * See the License for the specific language governing permissions and 16 | * limitations under the License. 17 | */ 18 | package org.apache.twill.zookeeper; 19 | 20 | import org.apache.zookeeper.data.Stat; 21 | 22 | import javax.annotation.Nullable; 23 | 24 | /** 25 | * Represents result of call to {@link ZKClient#getData(String, org.apache.zookeeper.Watcher)}. 26 | */ 27 | public interface NodeData { 28 | 29 | /** 30 | * @return The {@link Stat} of the node. 31 | */ 32 | Stat getStat(); 33 | 34 | /** 35 | * @return Data stored in the node, or {@code null} if there is no data. 36 | */ 37 | @Nullable 38 | byte[] getData(); 39 | } 40 | -------------------------------------------------------------------------------- /twill-zookeeper/src/main/java/org/apache/twill/zookeeper/OperationFuture.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Licensed to the Apache Software Foundation (ASF) under one 3 | * or more contributor license agreements. See the NOTICE file 4 | * distributed with this work for additional information 5 | * regarding copyright ownership. The ASF licenses this file 6 | * to you under the Apache License, Version 2.0 (the 7 | * "License"); you may not use this file except in compliance 8 | * with the License. You may obtain a copy of the License at 9 | * 10 | * http://www.apache.org/licenses/LICENSE-2.0 11 | * 12 | * Unless required by applicable law or agreed to in writing, software 13 | * distributed under the License is distributed on an "AS IS" BASIS, 14 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 15 | * See the License for the specific language governing permissions and 16 | * limitations under the License. 17 | */ 18 | package org.apache.twill.zookeeper; 19 | 20 | import com.google.common.util.concurrent.ListenableFuture; 21 | 22 | /** 23 | * A {@link ListenableFuture} that also provides the requested path for a operation. 24 | * 25 | * @param The result type returned by this Future's {@link #get()} method. 26 | */ 27 | public interface OperationFuture extends ListenableFuture { 28 | 29 | /** 30 | * @return The path being requested for the ZooKeeper operation. 31 | */ 32 | String getRequestPath(); 33 | } 34 | -------------------------------------------------------------------------------- /twill-zookeeper/src/main/java/org/apache/twill/zookeeper/RetryStrategy.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Licensed to the Apache Software Foundation (ASF) under one 3 | * or more contributor license agreements. See the NOTICE file 4 | * distributed with this work for additional information 5 | * regarding copyright ownership. The ASF licenses this file 6 | * to you under the Apache License, Version 2.0 (the 7 | * "License"); you may not use this file except in compliance 8 | * with the License. You may obtain a copy of the License at 9 | * 10 | * http://www.apache.org/licenses/LICENSE-2.0 11 | * 12 | * Unless required by applicable law or agreed to in writing, software 13 | * distributed under the License is distributed on an "AS IS" BASIS, 14 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 15 | * See the License for the specific language governing permissions and 16 | * limitations under the License. 17 | */ 18 | package org.apache.twill.zookeeper; 19 | 20 | /** 21 | * Provides strategy to use for operation retries. 22 | */ 23 | public interface RetryStrategy { 24 | 25 | /** 26 | * Defines ZooKeeper operation type that triggers retry. 27 | */ 28 | enum OperationType { 29 | CREATE, 30 | EXISTS, 31 | GET_CHILDREN, 32 | GET_DATA, 33 | SET_DATA, 34 | DELETE, 35 | SET_ACL, 36 | GET_ACL 37 | } 38 | 39 | /** 40 | * Returns the number of milliseconds to wait before retrying the operation. 41 | * 42 | * @param failureCount Number of times that the request has been failed. 43 | * @param startTime Timestamp in milliseconds that the request starts. 44 | * @param type Type of operation tried to perform. 45 | * @param path The path that the operation is acting on. 46 | * @return Number of milliseconds to wait before retrying the operation. Returning {@code 0} means 47 | * retry it immediately, while negative means abort the operation. 48 | */ 49 | long nextRetry(int failureCount, long startTime, OperationType type, String path); 50 | } 51 | -------------------------------------------------------------------------------- /twill-zookeeper/src/main/java/org/apache/twill/zookeeper/package-info.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Licensed to the Apache Software Foundation (ASF) under one 3 | * or more contributor license agreements. See the NOTICE file 4 | * distributed with this work for additional information 5 | * regarding copyright ownership. The ASF licenses this file 6 | * to you under the Apache License, Version 2.0 (the 7 | * "License"); you may not use this file except in compliance 8 | * with the License. You may obtain a copy of the License at 9 | * 10 | * http://www.apache.org/licenses/LICENSE-2.0 11 | * 12 | * Unless required by applicable law or agreed to in writing, software 13 | * distributed under the License is distributed on an "AS IS" BASIS, 14 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 15 | * See the License for the specific language governing permissions and 16 | * limitations under the License. 17 | */ 18 | 19 | /** 20 | * This package provides functionality for ZooKeeper interactions. 21 | */ 22 | package org.apache.twill.zookeeper; 23 | -------------------------------------------------------------------------------- /twill-zookeeper/src/test/resources/logback-test.xml: -------------------------------------------------------------------------------- 1 | 2 | 18 | 19 | 20 | 21 | 22 | 23 | %d{ISO8601} - %-5p [%t:%C{1}@%L] - %m%n 24 | 25 | 26 | 27 | 28 | 29 | 30 | 31 | 32 | 33 | 34 | --------------------------------------------------------------------------------