Skip to content
This repository has been archived by the owner on Feb 8, 2019. It is now read-only.

Commit

Permalink
GOSSIP-89 - Refactor gossip examples; common code in base class.
Browse files Browse the repository at this point in the history
  • Loading branch information
Terry-Weymouth committed Jul 18, 2017
1 parent 95cce48 commit 1c673c8
Show file tree
Hide file tree
Showing 7 changed files with 402 additions and 259 deletions.

This file was deleted.

@@ -0,0 +1,105 @@
/*
* 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.gossip.examples;

import java.io.IOException;

public class RunStandardExamples {

private static boolean WILL_READ = true;

private static boolean WILL_NOT_READ = false;

public static void main(String[] args) {
if ((args.length < 1) || args[0].equals("-h") || args[0].equals("--help") || args.length < 2) {
System.out.print(usage());
return;
}
try {
int example = intFromString(args[0]);
int channel = intFromString(args[1]);
if ((example < 1) || (example > 4) || (channel < 0) || (channel > 2)) {
System.out.print(usage());
return;
}
runExaple(example, channel);
} catch (Exception e) {
System.out.print(usage());
}
}

private static void runExaple(int exampleNumber, int channel) throws IOException {
String[] args = stanardArgs(channel, new String[4]);
if (exampleNumber == 1) {
StandAloneNode example = new StandAloneNode(args);
example.exec(WILL_NOT_READ);
} else if (exampleNumber == 2) {
StandAloneNodeCrdtOrSet example = new StandAloneNodeCrdtOrSet(args);
example.exec(WILL_READ);
} else if (exampleNumber == 3) {
StandAlonePNCounter example = new StandAlonePNCounter(args);
example.exec(WILL_READ);
} else if (exampleNumber == 4) {
args = extendedArgs(channel, new String[6]);
StandAloneDatacenterAndRack example = new StandAloneDatacenterAndRack(args);
example.exec(WILL_READ);
}
}

private static String[] stanardArgs(int channel, String[] args) {
// see README.md for examples
args[0] = "udp://localhost:1000" + channel;
args[1] = "" + channel;
args[2] = "udp://localhost:10000";
args[3] = "0";
return args;
}

private static String[] extendedArgs(int channel, String[] args) {
args = stanardArgs(channel, args);
// see README.md for examples
if (channel == 0) {
args[4] = "1";
args[5] = "2";
}
if (channel == 1) {
args[4] = "1";
args[5] = "3";
}
if (channel == 2) {
args[4] = "2";
args[5] = "2";
}
return args;
}

private static int intFromString(String string) {
return Integer.parseInt(string);
}

private static String usage() {
return "Select and run (usually in a seperate terminal window) \n"
+ "one of the the standard Examples,\n" + " 1. StandAloneNode\n"
+ " 2. StandAloneNodeCrdtOrSet\n" + " 3. StandAlonePNCounter\n"
+ " 4. StandAloneDatacenterAndRack\n" + "(See README.md in this modules)\n" + "\n"
+ "Usage: mvn exec:java -Dexec.mainClass=org.apache.gossip.examples.RunStandardExamples -Dexec.args=\"s c\"\n"
+ "where...\n" + " s - int - the example number from above\n"
+ " c - int - the channel number: 0, 1, or 2\n";
}

}
Expand Up @@ -18,8 +18,8 @@

package org.apache.gossip.examples;

import java.io.IOException;
import java.net.URI;
import java.net.UnknownHostException;
import java.util.Arrays;
import java.util.HashMap;
import java.util.Map;
Expand All @@ -30,12 +30,20 @@
import org.apache.gossip.manager.GossipManager;
import org.apache.gossip.manager.GossipManagerBuilder;

public class StandAloneDatacenterAndRack {
public class StandAloneDatacenterAndRack extends StandAloneExampleBase {

private static ExampleCommon common = new ExampleCommon();
public static void main(String[] args) throws InterruptedException, IOException {
StandAloneDatacenterAndRack example = new StandAloneDatacenterAndRack(args);
boolean willRead = true;
example.exec(willRead);
}

StandAloneDatacenterAndRack(String[] args) {
args = super.checkArgsForClearFlag(args);
initGossipManager(args);
}

public static void main(String[] args) throws UnknownHostException, InterruptedException {
args = common.checkArgsForClearFlag(args);
void initGossipManager(String[] args) {
GossipSettings s = new GossipSettings();
s.setWindowSize(1000);
s.setGossipInterval(100);
Expand All @@ -48,20 +56,17 @@ public static void main(String[] args) throws UnknownHostException, InterruptedE
props.put(DatacenterRackAwareActiveGossiper.DATACENTER, args[4]);
props.put(DatacenterRackAwareActiveGossiper.RACK, args[5]);
GossipManager manager = GossipManagerBuilder.newBuilder().cluster("mycluster")
.uri(URI.create(args[0]))
.id(args[1])
.gossipSettings(s)
.uri(URI.create(args[0])).id(args[1]).gossipSettings(s)
.gossipMembers(
Arrays.asList(new RemoteMember("mycluster", URI.create(args[2]), args[3])))
.properties(props)
.build();
.properties(props).build();
manager.init();
while (true) {
common.optionallyClearTerminal();
System.out.println("Live: " + manager.getLiveMembers());
System.out.println("Dead: " + manager.getDeadMembers());
Thread.sleep(2000);
}
setGossipService(manager);
}

@Override
void printValues(GossipManager gossipService) {
return;
}

}
@@ -0,0 +1,162 @@
/*
* 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.gossip.examples;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.net.URI;
import java.util.Collections;
import java.util.List;

import org.apache.gossip.GossipSettings;
import org.apache.gossip.LocalMember;
import org.apache.gossip.RemoteMember;
import org.apache.gossip.manager.GossipManager;
import org.apache.gossip.manager.GossipManagerBuilder;

abstract class StandAloneExampleBase {
private String lastInput = "{none}";

private boolean clearTerminalScreen = true;

private GossipManager gossipService = null;

abstract void printValues(GossipManager gossipService);

boolean processReadLoopInput(String line) {
return true;
}

void exec(boolean willRead) throws IOException {
gossipService.init();
startMonitorLoop(gossipService);
if (willRead) {
startBlockingReadLoop();
}
}

/*
* Look for -s in args. If there, suppress terminal-clear on write results: shift args for
* positional args, if necessary
*/
String[] checkArgsForClearFlag(String[] args) {
int pos = 0;
for (int i = 0; i < args.length; i++) {
if (args[i].equals("-s")) {
clearTerminalScreen = false;
} else {
// in the case of the -s flag, shift args
// down by one slot; this will end up with
// a duplicate entry in the last position of args,
// but this is ok, because it will be ignored
args[pos++] = args[i];
}
}
return args;
}

private void optionallyClearTerminal() {
if (clearTerminalScreen) {
System.out.print("\033[H\033[2J");
System.out.flush();
}
}

private void setLastInput(String input, boolean valid) {
lastInput = input;
if (!valid) {
lastInput += " (invalid)";
}
}

String getLastInput() {
return lastInput;
}

private void startMonitorLoop(GossipManager gossipService) {
new Thread(() -> {
while (true) {
optionallyClearTerminal();
printLiveMembers(gossipService);
printDeadMambers(gossipService);
printValues(gossipService);
try {
Thread.sleep(2000);
} catch (Exception ignore) {
}
}
}).start();
}

private void printLiveMembers(GossipManager gossipService) {
List<LocalMember> members = gossipService.getLiveMembers();
if (members.isEmpty()) {
System.out.println("Live: (none)");
return;
}
System.out.println("Live: " + members.get(0));
for (int i = 1; i < members.size(); i++) {
System.out.println(" : " + members.get(i));
}
}

private void printDeadMambers(GossipManager gossipService) {
List<LocalMember> members = gossipService.getDeadMembers();
if (members.isEmpty()) {
System.out.println("Dead: (none)");
return;
}
System.out.println("Dead: " + members.get(0));
for (int i = 1; i < members.size(); i++) {
System.out.println(" : " + members.get(i));
}
}

private void startBlockingReadLoop() throws IOException {
String line;
try (BufferedReader br = new BufferedReader(new InputStreamReader(System.in))) {
while ((line = br.readLine()) != null) {
System.out.println(line);
boolean valid = processReadLoopInput(line);
setLastInput(line, valid);
}
}
}

void initGossipManager(String[] args) {
GossipSettings s = new GossipSettings();
s.setWindowSize(1000);
s.setGossipInterval(100);
GossipManager gossipService = GossipManagerBuilder.newBuilder().cluster("mycluster")
.uri(URI.create(args[0])).id(args[1])
.gossipMembers(Collections
.singletonList(new RemoteMember("mycluster", URI.create(args[2]), args[3])))
.gossipSettings(s).build();
setGossipService(gossipService);
}

void setGossipService(GossipManager gossipService) {
this.gossipService = gossipService;
}

GossipManager getGossipManager() {
return this.gossipService;
}

}

0 comments on commit 1c673c8

Please sign in to comment.