Commit 34204c32 authored by NILANJAN DAW's avatar NILANJAN DAW

Added heartbeat service and follower autodiscovery

Added backend for in memory data storage
parent 2ff3bcac
<?xml version="1.0" encoding="UTF-8"?>
<project version="4">
<component name="GradleMigrationSettings" migrationVersion="1" />
<component name="GradleSettings">
<option name="linkedExternalProjectsSettings">
<GradleProjectSettings>
......
<?xml version="1.0" encoding="UTF-8"?>
<project version="4">
<component name="Palette2">
<group name="Swing">
<item class="com.intellij.uiDesigner.HSpacer" tooltip-text="Horizontal Spacer" icon="/com/intellij/uiDesigner/icons/hspacer.png" removable="false" auto-create-binding="false" can-attach-label="false">
<default-constraints vsize-policy="1" hsize-policy="6" anchor="0" fill="1" />
</item>
<item class="com.intellij.uiDesigner.VSpacer" tooltip-text="Vertical Spacer" icon="/com/intellij/uiDesigner/icons/vspacer.png" removable="false" auto-create-binding="false" can-attach-label="false">
<default-constraints vsize-policy="6" hsize-policy="1" anchor="0" fill="2" />
</item>
<item class="javax.swing.JPanel" icon="/com/intellij/uiDesigner/icons/panel.png" removable="false" auto-create-binding="false" can-attach-label="false">
<default-constraints vsize-policy="3" hsize-policy="3" anchor="0" fill="3" />
</item>
<item class="javax.swing.JScrollPane" icon="/com/intellij/uiDesigner/icons/scrollPane.png" removable="false" auto-create-binding="false" can-attach-label="true">
<default-constraints vsize-policy="7" hsize-policy="7" anchor="0" fill="3" />
</item>
<item class="javax.swing.JButton" icon="/com/intellij/uiDesigner/icons/button.png" removable="false" auto-create-binding="true" can-attach-label="false">
<default-constraints vsize-policy="0" hsize-policy="3" anchor="0" fill="1" />
<initial-values>
<property name="text" value="Button" />
</initial-values>
</item>
<item class="javax.swing.JRadioButton" icon="/com/intellij/uiDesigner/icons/radioButton.png" removable="false" auto-create-binding="true" can-attach-label="false">
<default-constraints vsize-policy="0" hsize-policy="3" anchor="8" fill="0" />
<initial-values>
<property name="text" value="RadioButton" />
</initial-values>
</item>
<item class="javax.swing.JCheckBox" icon="/com/intellij/uiDesigner/icons/checkBox.png" removable="false" auto-create-binding="true" can-attach-label="false">
<default-constraints vsize-policy="0" hsize-policy="3" anchor="8" fill="0" />
<initial-values>
<property name="text" value="CheckBox" />
</initial-values>
</item>
<item class="javax.swing.JLabel" icon="/com/intellij/uiDesigner/icons/label.png" removable="false" auto-create-binding="false" can-attach-label="false">
<default-constraints vsize-policy="0" hsize-policy="0" anchor="8" fill="0" />
<initial-values>
<property name="text" value="Label" />
</initial-values>
</item>
<item class="javax.swing.JTextField" icon="/com/intellij/uiDesigner/icons/textField.png" removable="false" auto-create-binding="true" can-attach-label="true">
<default-constraints vsize-policy="0" hsize-policy="6" anchor="8" fill="1">
<preferred-size width="150" height="-1" />
</default-constraints>
</item>
<item class="javax.swing.JPasswordField" icon="/com/intellij/uiDesigner/icons/passwordField.png" removable="false" auto-create-binding="true" can-attach-label="true">
<default-constraints vsize-policy="0" hsize-policy="6" anchor="8" fill="1">
<preferred-size width="150" height="-1" />
</default-constraints>
</item>
<item class="javax.swing.JFormattedTextField" icon="/com/intellij/uiDesigner/icons/formattedTextField.png" removable="false" auto-create-binding="true" can-attach-label="true">
<default-constraints vsize-policy="0" hsize-policy="6" anchor="8" fill="1">
<preferred-size width="150" height="-1" />
</default-constraints>
</item>
<item class="javax.swing.JTextArea" icon="/com/intellij/uiDesigner/icons/textArea.png" removable="false" auto-create-binding="true" can-attach-label="true">
<default-constraints vsize-policy="6" hsize-policy="6" anchor="0" fill="3">
<preferred-size width="150" height="50" />
</default-constraints>
</item>
<item class="javax.swing.JTextPane" icon="/com/intellij/uiDesigner/icons/textPane.png" removable="false" auto-create-binding="true" can-attach-label="true">
<default-constraints vsize-policy="6" hsize-policy="6" anchor="0" fill="3">
<preferred-size width="150" height="50" />
</default-constraints>
</item>
<item class="javax.swing.JEditorPane" icon="/com/intellij/uiDesigner/icons/editorPane.png" removable="false" auto-create-binding="true" can-attach-label="true">
<default-constraints vsize-policy="6" hsize-policy="6" anchor="0" fill="3">
<preferred-size width="150" height="50" />
</default-constraints>
</item>
<item class="javax.swing.JComboBox" icon="/com/intellij/uiDesigner/icons/comboBox.png" removable="false" auto-create-binding="true" can-attach-label="true">
<default-constraints vsize-policy="0" hsize-policy="2" anchor="8" fill="1" />
</item>
<item class="javax.swing.JTable" icon="/com/intellij/uiDesigner/icons/table.png" removable="false" auto-create-binding="true" can-attach-label="false">
<default-constraints vsize-policy="6" hsize-policy="6" anchor="0" fill="3">
<preferred-size width="150" height="50" />
</default-constraints>
</item>
<item class="javax.swing.JList" icon="/com/intellij/uiDesigner/icons/list.png" removable="false" auto-create-binding="true" can-attach-label="false">
<default-constraints vsize-policy="6" hsize-policy="2" anchor="0" fill="3">
<preferred-size width="150" height="50" />
</default-constraints>
</item>
<item class="javax.swing.JTree" icon="/com/intellij/uiDesigner/icons/tree.png" removable="false" auto-create-binding="true" can-attach-label="false">
<default-constraints vsize-policy="6" hsize-policy="6" anchor="0" fill="3">
<preferred-size width="150" height="50" />
</default-constraints>
</item>
<item class="javax.swing.JTabbedPane" icon="/com/intellij/uiDesigner/icons/tabbedPane.png" removable="false" auto-create-binding="true" can-attach-label="false">
<default-constraints vsize-policy="3" hsize-policy="3" anchor="0" fill="3">
<preferred-size width="200" height="200" />
</default-constraints>
</item>
<item class="javax.swing.JSplitPane" icon="/com/intellij/uiDesigner/icons/splitPane.png" removable="false" auto-create-binding="false" can-attach-label="false">
<default-constraints vsize-policy="3" hsize-policy="3" anchor="0" fill="3">
<preferred-size width="200" height="200" />
</default-constraints>
</item>
<item class="javax.swing.JSpinner" icon="/com/intellij/uiDesigner/icons/spinner.png" removable="false" auto-create-binding="true" can-attach-label="true">
<default-constraints vsize-policy="0" hsize-policy="6" anchor="8" fill="1" />
</item>
<item class="javax.swing.JSlider" icon="/com/intellij/uiDesigner/icons/slider.png" removable="false" auto-create-binding="true" can-attach-label="false">
<default-constraints vsize-policy="0" hsize-policy="6" anchor="8" fill="1" />
</item>
<item class="javax.swing.JSeparator" icon="/com/intellij/uiDesigner/icons/separator.png" removable="false" auto-create-binding="false" can-attach-label="false">
<default-constraints vsize-policy="6" hsize-policy="6" anchor="0" fill="3" />
</item>
<item class="javax.swing.JProgressBar" icon="/com/intellij/uiDesigner/icons/progressbar.png" removable="false" auto-create-binding="true" can-attach-label="false">
<default-constraints vsize-policy="0" hsize-policy="6" anchor="0" fill="1" />
</item>
<item class="javax.swing.JToolBar" icon="/com/intellij/uiDesigner/icons/toolbar.png" removable="false" auto-create-binding="false" can-attach-label="false">
<default-constraints vsize-policy="0" hsize-policy="6" anchor="0" fill="1">
<preferred-size width="-1" height="20" />
</default-constraints>
</item>
<item class="javax.swing.JToolBar$Separator" icon="/com/intellij/uiDesigner/icons/toolbarSeparator.png" removable="false" auto-create-binding="false" can-attach-label="false">
<default-constraints vsize-policy="0" hsize-policy="0" anchor="0" fill="1" />
</item>
<item class="javax.swing.JScrollBar" icon="/com/intellij/uiDesigner/icons/scrollbar.png" removable="false" auto-create-binding="true" can-attach-label="false">
<default-constraints vsize-policy="6" hsize-policy="0" anchor="0" fill="2" />
</item>
</group>
</component>
</project>
\ No newline at end of file
package hpdos;
public class ConfigConstants {
public static final String HOST = "localhost";
public static final int PORT = 8080;
public static final int HEARTBEAT_INTERVAL = 500;
// Backend types 300-399
public static final int BACKEND_IN_MEMORY = 300;
public static final int LSM_BACKEND = 301;
public static final int BINARY_TREE_BACKEND = 302;
}
......@@ -3,14 +3,42 @@
*/
package hpdos;
import hpdos.grpc.HeartbeatRequest;
import hpdos.grpc.HeartbeatResponse;
import hpdos.grpc.HeartbeatServiceGrpc;
import hpdos.handler.HeartbeatHandler;
import hpdos.handler.NetworkHandler;
import hpdos.lib.MasterFollower;
import hpdos.lib.MemoryStorage;
import hpdos.lib.StorageService;
import hpdos.messageformat.MessageConstants;
import io.grpc.ManagedChannel;
import io.grpc.ManagedChannelBuilder;
import io.grpc.Server;
import io.grpc.ServerBuilder;
import java.io.IOException;
import java.util.Arrays;
import java.util.HashMap;
import java.util.Timer;
import java.util.TimerTask;
import java.util.UUID;
public class MetadataServer {
private Server server;
private final HashMap<String, MasterFollower> followers;
private final String serverID;
private boolean isMaster = false;
private int port;
private final String host;
private StorageService storageService;
public MetadataServer() {
this.followers = new HashMap<>();
this.serverID = UUID.randomUUID().toString();
this.port = 10000 + (int)(Math.random() * 40000);
this.host = "localhost";
}
public String getGreeting() {
return "Hello World!";
}
......@@ -18,6 +46,7 @@ public class MetadataServer {
public boolean startServer() {
server = ServerBuilder.forPort(ConfigConstants.PORT)
.addService(new NetworkHandler())
.addService(new HeartbeatHandler(followers, serverID))
.build();
try {
server.start();
......@@ -38,15 +67,76 @@ public class MetadataServer {
}
}
private void announceToMaster() {
final ManagedChannel channel = ManagedChannelBuilder.
forAddress(ConfigConstants.HOST, ConfigConstants.PORT)
.usePlaintext()
.build();
HeartbeatServiceGrpc.HeartbeatServiceBlockingStub stub = HeartbeatServiceGrpc.newBlockingStub(channel);
HeartbeatRequest.Builder heartbeat = HeartbeatRequest.newBuilder();
heartbeat.setPacketType(MessageConstants.PACKET_METADATA_REQUEST);
heartbeat.setOperationType(MessageConstants.MASTER_HEARTBEAT);
heartbeat.setFollowerID(this.serverID);
heartbeat.setIp(this.host);
heartbeat.setPort(this.port);
try {
HeartbeatResponse response = stub.heartbeat(heartbeat.build());
} catch (Exception e) {
System.out.println("Metadata Master not found, electing self as master");
this.isMaster = true;
this.port = ConfigConstants.PORT;
}
channel.shutdown();
}
private void startHeartbeatService() {
Timer timer = new Timer();
timer.scheduleAtFixedRate(new TimerTask() {
@Override
public void run() {
announceToMaster();
}
}, ConfigConstants.HEARTBEAT_INTERVAL, ConfigConstants.HEARTBEAT_INTERVAL);
}
private boolean initStorage(int backend) {
switch (backend) {
case ConfigConstants.BACKEND_IN_MEMORY:
this.storageService = new MemoryStorage();
return true;
case ConfigConstants.BINARY_TREE_BACKEND:
case ConfigConstants.LSM_BACKEND:
default: return false;
}
}
public static void main(String[] args) {
MetadataServer metaDataServer = new MetadataServer();
System.out.println(metaDataServer.getGreeting());
boolean status = metaDataServer.startServer();
if (status) {
System.out.println("Starting RPC server at: " + ConfigConstants.PORT);
metaDataServer.blockForIO();
System.out.println("Starting Metadata service");
System.out.println("Initialising storage service");
boolean status = metaDataServer.initStorage(ConfigConstants.BACKEND_IN_MEMORY);
if (!status) {
System.out.println("Storage server initialisation error");
return;
}
else
System.out.println("Failed to create server");
System.out.println("Searching for MetadataMaster");
metaDataServer.announceToMaster();
if (metaDataServer.isMaster) {
status = metaDataServer.startServer();
System.out.println("Master ID: " + metaDataServer.serverID);
if (status) {
System.out.println("Starting RPC server at: " + ConfigConstants.PORT);
metaDataServer.blockForIO();
}
else
System.out.println("Failed to create server");
} else {
System.out.println("Master Node detected.\nStarting heartbeat service");
metaDataServer.startHeartbeatService();
}
}
}
package hpdos.handler;
import hpdos.grpc.HeartbeatRequest;
import hpdos.grpc.HeartbeatResponse;
import hpdos.grpc.HeartbeatServiceGrpc;
import hpdos.lib.MasterFollower;
import hpdos.messageformat.MessageConstants;
import io.grpc.stub.StreamObserver;
import java.io.UnsupportedEncodingException;
import java.security.NoSuchAlgorithmException;
import java.security.Timestamp;
import java.time.Instant;
import java.util.ArrayList;
import java.util.HashMap;
public class HeartbeatHandler extends HeartbeatServiceGrpc.HeartbeatServiceImplBase {
private final HashMap<String, MasterFollower> followers;
private String serverID;
public HeartbeatHandler(HashMap<String, MasterFollower> followers, String serverID) {
this.followers = followers;
this.serverID = serverID;
}
@Override
public void heartbeat(HeartbeatRequest request, StreamObserver<HeartbeatResponse> heartbeatResponseStreamObserver) {
if (!followers.containsKey(request.getFollowerID())) {
System.out.println("New follower autodiscovery. Added to system " + request);
followers.put(request.getFollowerID(), new MasterFollower(request.getIp(),
request.getPort(), request.getFollowerID()));
} else {
MasterFollower follower = followers.get(request.getFollowerID());
follower.setLastSeen(Instant.now().getEpochSecond());
}
HeartbeatResponse.Builder response = HeartbeatResponse.newBuilder();
response.setPacketType(MessageConstants.PACKET_METADATA_RESPONSE);
response.setOperationType(MessageConstants.MASTER_HEARTBEAT);
response.setMasterID(this.serverID);
heartbeatResponseStreamObserver.onNext(response.build());
heartbeatResponseStreamObserver.onCompleted();
}
}
package hpdos;
package hpdos.handler;
import hpdos.grpc.*;
import hpdos.messageformat.*;
import io.grpc.stub.StreamObserver;
import java.util.ArrayList;
public class NetworkHandler extends NetworkServiceGrpc.NetworkServiceImplBase {
......
package hpdos.lib;
import java.time.Instant;
public class MasterFollower {
private String ip;
private int port;
private final String followerID;
private long lastSeen;
public MasterFollower(String ip, int port, String followerID) {
this.ip = ip;
this.port = port;
this.followerID = followerID;
this.lastSeen = Instant.now().getEpochSecond();
}
public String getIp() {
return ip;
}
public void setIp(String ip) {
this.ip = ip;
}
public int getPort() {
return port;
}
public void setPort(int port) {
this.port = port;
}
public String getFollowerID() {
return followerID;
}
public long getLastSeen() {
return lastSeen;
}
public void setLastSeen(long lastSeen) {
this.lastSeen = lastSeen;
}
@Override
public String toString() {
return "MasterFollower {" +
"ip='" + ip + '\'' +
", port=" + port +
", followerID='" + followerID + '\'' +
'}';
}
}
package hpdos.lib;
import java.util.HashMap;
public class MemoryStorage implements StorageService{
private final HashMap<String, String> memoryKVStore;
public MemoryStorage() {
this.memoryKVStore = new HashMap<>();
}
@Override
public String put(String key, String value) {
try {
return memoryKVStore.put(key, value);
} catch (Exception e) {
return null;
}
}
@Override
public String getByKey(String key) {
return memoryKVStore.get(key);
}
}
package hpdos.lib;
public interface StorageService {
String put(String key, String value);
String getByKey(String key);
}
......@@ -9,7 +9,8 @@ public class MessageConstants {
public static final boolean STATUS_OK = true;
public static final boolean STATUS_FAIL = false;
// 101 to 199 - HPDOS System internal operations
// 100 to 199 - HPDOS System internal operations
public static final int MASTER_HEARTBEAT = 100;
public static final int METADATA_LOOKUP_REQUEST = 101;
public static final int METADATA_LOOKUP_RESPONSE = 102;
}
syntax = "proto3";
package hpdos.grpc;
option java_multiple_files = true;
option java_package = "hpdos.grpc";
option java_outer_classname = "Heartbeat";
service HeartbeatService {
/**
Exchange heartbeat
*/
rpc heartbeat(HeartbeatRequest) returns (HeartbeatResponse) {}
}
message HeartbeatRequest {
int32 packetType = 1;
int32 operationType = 2;
string ip = 3;
int32 port = 4;
string followerID = 5;
}
message HeartbeatResponse {
int32 packetType = 1;
int32 operationType = 2;
bool status = 3;
string masterID = 4;
}
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment