-
Notifications
You must be signed in to change notification settings - Fork 204
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[Part 10]: Task Executor Starter and other related classes (#160)
* [Part 10]: Task Executor Starter and other related classes TaskExecutorStarter is the starting point for the task executor from the worker side. Initializing this class and starting it as part of the runtime framework (guice/springboot) would start the task executor thread capable of running any task from the client side. Co-authored-by: Sundaram Ananthanarayanan <[email protected]>
- Loading branch information
1 parent
45d804d
commit ecf0d4e
Showing
8 changed files
with
368 additions
and
19 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
39 changes: 39 additions & 0 deletions
39
...s-control-plane-core/src/main/java/io/mantisrx/server/core/MantisAkkaRpcSystemLoader.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,39 @@ | ||
/* | ||
* Copyright 2022 Netflix, Inc. | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
|
||
package io.mantisrx.server.core; | ||
|
||
import org.apache.flink.configuration.Configuration; | ||
import org.apache.flink.runtime.rpc.RpcSystem; | ||
import org.apache.flink.runtime.rpc.RpcSystemLoader; | ||
import org.apache.flink.runtime.rpc.akka.AkkaRpcSystem; | ||
|
||
/** | ||
* RpcSystemLoader for mantis task executor and other services that need to expose an RPC API. | ||
* This particular implementation uses the akka RPC implementation under the hood. | ||
*/ | ||
public class MantisAkkaRpcSystemLoader implements RpcSystemLoader { | ||
|
||
private static final RpcSystem INSTANCE = new AkkaRpcSystem(); | ||
@Override | ||
public RpcSystem loadRpcSystem(Configuration config) { | ||
return INSTANCE; | ||
} | ||
|
||
public static RpcSystem getInstance() { | ||
return INSTANCE; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
36 changes: 36 additions & 0 deletions
36
...erver/mantis-server-worker/src/main/java/io/mantisrx/server/worker/FileSystemFactory.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,36 @@ | ||
/* | ||
* Copyright 2022 Netflix, Inc. | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
package io.mantisrx.server.worker; | ||
|
||
import java.io.IOException; | ||
import java.net.URI; | ||
import org.apache.hadoop.fs.FileSystem; | ||
|
||
public interface FileSystemFactory { | ||
/** Gets the scheme of the file system created by this factory. */ | ||
String getScheme(); | ||
|
||
/** | ||
* Creates a new file system for the given file system URI. The URI describes the type of file | ||
* system (via its scheme) and optionally the authority (for example the host) of the file | ||
* system. | ||
* | ||
* @param fsUri The URI that describes the file system. | ||
* @return A new instance of the specified file system. | ||
* @throws IOException Thrown if the file system could not be instantiated. | ||
*/ | ||
FileSystem create(URI fsUri) throws IOException; | ||
} |
60 changes: 60 additions & 0 deletions
60
...r/mantis-server-worker/src/main/java/io/mantisrx/server/worker/FileSystemInitializer.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,60 @@ | ||
/* | ||
* Copyright 2022 Netflix, Inc. | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
package io.mantisrx.server.worker; | ||
|
||
import java.io.IOException; | ||
import java.net.URI; | ||
import java.util.HashMap; | ||
import java.util.Iterator; | ||
import java.util.Map; | ||
import java.util.ServiceLoader; | ||
import lombok.extern.slf4j.Slf4j; | ||
import org.apache.hadoop.fs.FileSystem; | ||
|
||
@Slf4j | ||
public class FileSystemInitializer { | ||
|
||
private static final Map<String, FileSystemFactory> FS_FACTORIES = new HashMap<>(); | ||
|
||
static { | ||
FileSystemInitializer.initialize(); | ||
} | ||
|
||
/** | ||
* initialize needs to be called at the start of the JVM. | ||
*/ | ||
public static void initialize() { | ||
FS_FACTORIES.clear(); | ||
|
||
// let's get all the registered implementations | ||
Iterator<FileSystemFactory> fileSystemFactoryIterator = | ||
ServiceLoader.load(FileSystemFactory.class).iterator(); | ||
fileSystemFactoryIterator.forEachRemaining(fileSystemFactory -> { | ||
log.info("Initializing FileSystem Factory {}", fileSystemFactory); | ||
FS_FACTORIES.putIfAbsent(fileSystemFactory.getScheme(), fileSystemFactory); | ||
}); | ||
} | ||
|
||
public static FileSystem create(URI fsUri) throws IOException { | ||
FileSystemFactory factory = FS_FACTORIES.get(fsUri.getScheme()); | ||
if (factory != null) { | ||
return factory.create(fsUri); | ||
} else { | ||
throw new IllegalArgumentException( | ||
String.format("Unknown schema %s", fsUri.getScheme())); | ||
} | ||
} | ||
} |
173 changes: 173 additions & 0 deletions
173
...ver/mantis-server-worker/src/main/java/io/mantisrx/server/worker/TaskExecutorStarter.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,173 @@ | ||
/* | ||
* Copyright 2022 Netflix, Inc. | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
|
||
package io.mantisrx.server.worker; | ||
|
||
import com.mantisrx.common.utils.Services; | ||
import io.mantisrx.server.core.MantisAkkaRpcSystemLoader; | ||
import io.mantisrx.server.master.client.HighAvailabilityServices; | ||
import io.mantisrx.server.master.client.HighAvailabilityServicesUtil; | ||
import io.mantisrx.server.worker.config.WorkerConfiguration; | ||
import io.mantisrx.shaded.com.google.common.base.Preconditions; | ||
import io.mantisrx.shaded.com.google.common.util.concurrent.AbstractIdleService; | ||
import io.mantisrx.shaded.com.google.common.util.concurrent.MoreExecutors; | ||
import java.time.Clock; | ||
import java.util.Optional; | ||
import javax.annotation.Nullable; | ||
import lombok.AccessLevel; | ||
import lombok.RequiredArgsConstructor; | ||
import lombok.extern.slf4j.Slf4j; | ||
import org.apache.flink.configuration.Configuration; | ||
import org.apache.flink.runtime.rpc.RpcService; | ||
import org.apache.flink.runtime.rpc.RpcSystem; | ||
import org.apache.flink.runtime.rpc.RpcUtils; | ||
|
||
/** | ||
* TaskExecutorStarter class represents the starting point for a task executor. | ||
* Use the {@link TaskExecutorStarterBuilder} to build {@link TaskExecutorStarter}. | ||
* Once the service is build, start and stop it during the lifecycle of your runtime framework such as spring. | ||
*/ | ||
@RequiredArgsConstructor(access = AccessLevel.PRIVATE) | ||
@Slf4j | ||
public class TaskExecutorStarter extends AbstractIdleService { | ||
|
||
private final TaskExecutor taskExecutor; | ||
private final HighAvailabilityServices highAvailabilityServices; | ||
|
||
@Override | ||
protected void startUp() { | ||
highAvailabilityServices.startAsync().awaitRunning(); | ||
|
||
taskExecutor.start(); | ||
} | ||
|
||
@Override | ||
protected void shutDown() throws Exception { | ||
taskExecutor | ||
.closeAsync() | ||
.exceptionally(throwable -> null) | ||
.thenCompose(dontCare -> Services.stopAsync(highAvailabilityServices, | ||
MoreExecutors.directExecutor())) | ||
.get(); | ||
} | ||
|
||
public static TaskExecutorStarterBuilder builder(WorkerConfiguration workerConfiguration) { | ||
return new TaskExecutorStarterBuilder(workerConfiguration); | ||
} | ||
|
||
@SuppressWarnings("unused") | ||
public static class TaskExecutorStarterBuilder { | ||
private final WorkerConfiguration workerConfiguration; | ||
private Configuration configuration; | ||
@Nullable | ||
private RpcSystem rpcSystem; | ||
@Nullable | ||
private RpcService rpcService; | ||
@Nullable | ||
private ClassLoaderHandle classLoaderHandle; | ||
private final HighAvailabilityServices highAvailabilityServices; | ||
@Nullable | ||
private SinkSubscriptionStateHandler.Factory sinkSubscriptionHandlerFactory; | ||
|
||
private TaskExecutorStarterBuilder(WorkerConfiguration workerConfiguration) { | ||
this.workerConfiguration = workerConfiguration; | ||
this.configuration = new Configuration(); | ||
this.highAvailabilityServices = HighAvailabilityServicesUtil.createHAServices(workerConfiguration); | ||
} | ||
|
||
public TaskExecutorStarterBuilder configuration(Configuration configuration) { | ||
this.configuration = configuration; | ||
return this; | ||
} | ||
|
||
public TaskExecutorStarterBuilder rpcSystem(RpcSystem rpcSystem) { | ||
Preconditions.checkNotNull(rpcSystem); | ||
this.rpcSystem = rpcSystem; | ||
return this; | ||
} | ||
|
||
private RpcSystem getRpcSystem() { | ||
if (this.rpcSystem == null) { | ||
return MantisAkkaRpcSystemLoader.getInstance(); | ||
} else { | ||
return this.rpcSystem; | ||
} | ||
} | ||
|
||
public TaskExecutorStarterBuilder rpcService(RpcService rpcService) { | ||
Preconditions.checkNotNull(rpcService); | ||
this.rpcService = rpcService; | ||
return this; | ||
} | ||
|
||
private RpcService getRpcService() throws Exception { | ||
if (this.rpcService == null) { | ||
return RpcUtils.createRemoteRpcService( | ||
getRpcSystem(), | ||
configuration, | ||
workerConfiguration.getExternalAddress(), | ||
workerConfiguration.getExternalPortRange(), | ||
workerConfiguration.getBindAddress(), | ||
Optional.ofNullable(workerConfiguration.getBindPort())); | ||
} else { | ||
return this.rpcService; | ||
} | ||
} | ||
|
||
public TaskExecutorStarterBuilder classLoaderHandle(ClassLoaderHandle classLoaderHandle) { | ||
this.classLoaderHandle = classLoaderHandle; | ||
return this; | ||
} | ||
|
||
private ClassLoaderHandle getClassLoaderHandle() throws Exception { | ||
if (this.classLoaderHandle == null) { | ||
return new BlobStoreAwareClassLoaderHandle( | ||
BlobStore.forHadoopFileSystem( | ||
workerConfiguration.getBlobStoreArtifactDir(), | ||
workerConfiguration.getLocalStorageDir())); | ||
} else { | ||
return this.classLoaderHandle; | ||
} | ||
} | ||
|
||
public TaskExecutorStarterBuilder sinkSubscriptionHandlerFactory(SinkSubscriptionStateHandler.Factory sinkSubscriptionHandlerFactory) { | ||
this.sinkSubscriptionHandlerFactory = sinkSubscriptionHandlerFactory; | ||
return this; | ||
} | ||
|
||
private SinkSubscriptionStateHandler.Factory getSinkSubscriptionHandlerFactory() { | ||
if (this.sinkSubscriptionHandlerFactory == null) { | ||
return SinkSubscriptionStateHandler.Factory.forEphemeralJobsThatNeedToBeKilledInAbsenceOfSubscriber( | ||
highAvailabilityServices.getMasterClientApi(), | ||
Clock.systemDefaultZone()); | ||
} else { | ||
return this.sinkSubscriptionHandlerFactory; | ||
} | ||
} | ||
|
||
public TaskExecutorStarter build() throws Exception { | ||
final TaskExecutor taskExecutor = | ||
new TaskExecutor( | ||
getRpcService(), | ||
workerConfiguration, | ||
highAvailabilityServices, | ||
getClassLoaderHandle(), | ||
getSinkSubscriptionHandlerFactory()); | ||
|
||
return new TaskExecutorStarter(taskExecutor, highAvailabilityServices); | ||
} | ||
} | ||
} |
Oops, something went wrong.