use of org.apache.zeppelin.cluster.ClusterCallback in project zeppelin by apache.
the class ClusterInterpreterLauncher method launchDirectly.
@Override
public InterpreterClient launchDirectly(InterpreterLaunchContext context) throws IOException {
LOGGER.info("Launching Interpreter: " + context.getInterpreterSettingGroup());
this.context = context;
this.properties = context.getProperties();
int connectTimeout = getConnectTimeout();
String intpGroupId = context.getInterpreterGroupId();
// connect exist Interpreter Process
InterpreterClient intpClient = clusterServer.getIntpProcessStatus(intpGroupId, 3000, new ClusterCallback<HashMap<String, Object>>() {
@Override
public InterpreterClient online(HashMap<String, Object> result) {
String intpTserverHost = (String) result.get(INTP_TSERVER_HOST);
int intpTserverPort = (int) result.get(INTP_TSERVER_PORT);
return new RemoteInterpreterRunningProcess(context.getInterpreterSettingName(), context.getInterpreterGroupId(), connectTimeout, getConnectPoolSize(), context.getIntpEventServerHost(), context.getIntpEventServerPort(), intpTserverHost, intpTserverPort, false);
}
@Override
public void offline() {
LOGGER.info("interpreter {} is not exist!", intpGroupId);
}
});
if (null != intpClient) {
return intpClient;
}
// No process was found for the InterpreterGroup ID
String srvHost = null;
int srvPort = 0;
HashMap<String, Object> meta = clusterServer.getIdleNodeMeta();
if (null == meta) {
LOGGER.error("Don't get idle node meta, launch interpreter on local.");
InterpreterClient clusterIntpProcess = createInterpreterProcess(context);
try {
clusterIntpProcess.start(context.getUserName());
} catch (IOException e) {
LOGGER.error(e.getMessage(), e);
return clusterIntpProcess;
}
} else {
srvHost = (String) meta.get(SERVER_HOST);
String localhost = RemoteInterpreterUtils.findAvailableHostAddress();
if (localhost.equalsIgnoreCase(srvHost)) {
LOGGER.info("launch interpreter on local");
InterpreterClient clusterIntpProcess = createInterpreterProcess(context);
try {
clusterIntpProcess.start(context.getUserName());
} catch (IOException e) {
LOGGER.error(e.getMessage(), e);
return clusterIntpProcess;
}
} else {
// launch interpreter on cluster
srvPort = (int) meta.get(SERVER_PORT);
Gson gson = new Gson();
String sContext = gson.toJson(context);
Map<String, Object> mapEvent = new HashMap<>();
mapEvent.put(CLUSTER_EVENT, CREATE_INTP_PROCESS);
mapEvent.put(CLUSTER_EVENT_MSG, sContext);
String strEvent = gson.toJson(mapEvent);
// Notify other server in the cluster that the resource is idle to create an interpreter
clusterServer.unicastClusterEvent(srvHost, srvPort, ClusterManagerServer.CLUSTER_INTP_EVENT_TOPIC, strEvent);
}
}
// Find the ip and port of thrift registered by the remote interpreter process
// through the cluster metadata
String finalSrvHost = srvHost;
int finalSrvPort = srvPort;
intpClient = clusterServer.getIntpProcessStatus(intpGroupId, connectTimeout, new ClusterCallback<HashMap<String, Object>>() {
@Override
public InterpreterClient online(HashMap<String, Object> result) {
// connect exist Interpreter Process
String intpTserverHost = (String) result.get(INTP_TSERVER_HOST);
int intpTserverPort = (int) result.get(INTP_TSERVER_PORT);
return new RemoteInterpreterRunningProcess(context.getInterpreterSettingName(), context.getInterpreterGroupId(), connectTimeout, getConnectPoolSize(), context.getIntpEventServerHost(), context.getIntpEventServerPort(), intpTserverHost, intpTserverPort, false);
}
@Override
public void offline() {
String errorInfo = String.format("Creating process %s failed on remote server %s:%d", intpGroupId, finalSrvHost, finalSrvPort);
LOGGER.error(errorInfo);
}
});
if (null == intpClient) {
String errorInfo = String.format("Creating process %s failed on remote server %s:%d", intpGroupId, srvHost, srvPort);
throw new IOException(errorInfo);
} else {
return intpClient;
}
}
Aggregations