This repository has been archived by the owner on Feb 8, 2019. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 110
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
GOSSIP-89 - Refactor gossip examples; common code in base class.
- Loading branch information
1 parent
95cce48
commit 1c673c8
Showing
7 changed files
with
402 additions
and
259 deletions.
There are no files selected for viewing
50 changes: 0 additions & 50 deletions
50
gossip-examples/src/main/java/org/apache/gossip/examples/ExampleCommon.java
This file was deleted.
Oops, something went wrong.
105 changes: 105 additions & 0 deletions
105
gossip-examples/src/main/java/org/apache/gossip/examples/RunStandardExamples.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,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"; | ||
} | ||
|
||
} |
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
162 changes: 162 additions & 0 deletions
162
gossip-examples/src/main/java/org/apache/gossip/examples/StandAloneExampleBase.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,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; | ||
} | ||
|
||
} |
Oops, something went wrong.