add zeppelin hearbeat

timeout is 5 mins, pinging all connected notes every 4 mins
This commit is contained in:
Khalid Huseynov 2016-05-24 15:58:57 +09:00
parent 6c3aff2f1b
commit 8de3d1fe05
2 changed files with 67 additions and 1 deletions

View file

@ -30,7 +30,10 @@ import org.apache.zeppelin.notebook.repo.zeppelinhub.security.Authentication;
import org.apache.zeppelin.notebook.repo.zeppelinhub.websocket.listener.ZeppelinWebsocket;
import org.apache.zeppelin.notebook.repo.zeppelinhub.websocket.protocol.ZeppelinhubMessage;
import org.apache.zeppelin.notebook.repo.zeppelinhub.websocket.scheduler.SchedulerService;
import org.apache.zeppelin.notebook.repo.zeppelinhub.websocket.scheduler.ZeppelinHeartbeat;
import org.apache.zeppelin.notebook.repo.zeppelinhub.websocket.scheduler.ZeppelinHubHeartbeat;
import org.apache.zeppelin.notebook.socket.Message;
import org.apache.zeppelin.notebook.socket.Message.OP;
import org.eclipse.jetty.util.ssl.SslContextFactory;
import org.eclipse.jetty.websocket.api.Session;
import org.eclipse.jetty.websocket.client.ClientUpgradeRequest;
@ -53,8 +56,9 @@ public class ZeppelinClient {
private static Gson gson;
private ConcurrentHashMap<String, Session> zeppelinConnectionMap;
private static ZeppelinClient instance = null;
private SchedulerService schedulerService;
private Authentication authModule;
private static final int min = 60;
public static ZeppelinClient initialize(String zeppelinUrl, String token,
ZeppelinConfiguration conf) {
@ -74,6 +78,7 @@ public class ZeppelinClient {
wsClient = createNewWebsocketClient();
gson = new Gson();
zeppelinConnectionMap = new ConcurrentHashMap<>();
schedulerService = SchedulerService.getInstance();
authModule = Authentication.initialize(token, conf);
if (authModule != null) {
SchedulerService.getInstance().addOnce(authModule, 10);
@ -84,6 +89,7 @@ public class ZeppelinClient {
private WebSocketClient createNewWebsocketClient() {
SslContextFactory sslContextFactory = new SslContextFactory();
WebSocketClient client = new WebSocketClient(sslContextFactory);
client.setMaxIdleTimeout(5 * min * 1000);
//TODO(khalid): other client settings
return client;
}
@ -92,6 +98,7 @@ public class ZeppelinClient {
try {
if (wsClient != null) {
wsClient.start();
addRoutines();
} else {
LOG.warn("Cannot start zeppelin websocket client - isn't initialized");
}
@ -100,6 +107,10 @@ public class ZeppelinClient {
}
}
private void addRoutines() {
schedulerService.add(ZeppelinHeartbeat.newInstance(this), 15, 4 * min);
}
public void stop() {
try {
if (wsClient != null) {
@ -258,6 +269,17 @@ public class ZeppelinClient {
LOG.info("Removed all Zeppelin ws connections");
}
public void pingAllNotes() {
for (Map.Entry<String, Session> entry: zeppelinConnectionMap.entrySet()) {
if (isSessionOpen(entry.getValue())) {
send(new Message(OP.PING), entry.getKey());
} else {
// for cleanup
zeppelinConnectionMap.remove(entry.getKey());
}
}
}
public int countConnectedNotes() {
return zeppelinConnectionMap.size();
}

View file

@ -0,0 +1,44 @@
/*
* Licensed to the Apache Software Foundation (ASF) under one or more
* contributor license agreements. See the NOTICE file distributed with
* this work for additional information regarding copyright ownership.
* The ASF licenses this file to You 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 org.apache.zeppelin.notebook.repo.zeppelinhub.websocket.scheduler;
import org.apache.zeppelin.notebook.repo.zeppelinhub.websocket.ZeppelinClient;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
/**
* Routine that sends PING to all connected Zeppelin ws connections.
*
*/
public class ZeppelinHeartbeat implements Runnable {
private static final Logger LOG = LoggerFactory.getLogger(ZeppelinHubHeartbeat.class);
private ZeppelinClient client;
public static ZeppelinHeartbeat newInstance(ZeppelinClient client) {
return new ZeppelinHeartbeat(client);
}
private ZeppelinHeartbeat(ZeppelinClient client) {
this.client = client;
}
@Override
public void run() {
LOG.debug("Sending PING to all connected Zeppelin notes");
client.pingAllNotes();
}
}