org.jgroups.util.Util Java Examples
The following examples show how to use
org.jgroups.util.Util.
You can vote up the ones you like or vote down the ones you don't like,
and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source File: CounterServiceDemo.java From jgroups-raft with Apache License 2.0 | 6 votes |
void start(String props, String name, long repl_timeout, boolean allow_dirty_reads, boolean follower) throws Exception { ch=new JChannel(props).name(name); counter_service=new CounterService(ch).raftId(name).replTimeout(repl_timeout).allowDirtyReads(allow_dirty_reads); if(follower) disableElections(ch); ch.setReceiver(new ReceiverAdapter() { public void viewAccepted(View view) { System.out.println("-- view: " + view); } }); try { ch.connect("cntrs"); loop(); } finally { Util.close(ch); } }
Example #2
Source File: JGroupsUtils.java From unitime with Apache License 2.0 | 6 votes |
private static String _getProperty(String var, String default_value) { if(var == null) return null; List<String> list=Util.parseCommaDelimitedStrings(var); if(list == null || list.isEmpty()) { list=new ArrayList<String>(1); list.add(var); } String retval=null; for(String prop: list) { try { retval=ApplicationProperties.getProperty(prop); if(retval != null) return retval; } catch(Throwable e) { } } return default_value; }
Example #3
Source File: Leader.java From jgroups-raft with Apache License 2.0 | 6 votes |
@Override protected void handleAppendEntriesResponse(Address sender, int term, AppendResult result) { RequestTable<String> reqtab=raft.request_table; if(reqtab == null) throw new IllegalStateException("request table cannot be null in leader"); ExtendedUUID uuid=(ExtendedUUID)sender; String sender_raft_id=Util.bytesToString(uuid.get(RAFT.raft_id_key)); raft.getLog().trace("%s: received AppendEntries response from %s for term %d: %s", raft.local_addr, sender, term, result); if(result.success) { raft.commit_table.update(sender, result.getIndex(), result.getIndex()+1, result.commit_index, false); if(reqtab.add(result.index, sender_raft_id, raft.majority())) raft.handleCommit(result.index); } else raft.commit_table.update(sender, 0, result.getIndex(), result.commit_index, true); }
Example #4
Source File: REDIRECT.java From jgroups-raft with Apache License 2.0 | 6 votes |
protected CompletableFuture<byte[]> changeServer(String name, boolean add) throws Exception { Address leader=leader("addServer()/removeServer()"); // we are the current leader: pass the call to the RAFT protocol if(Objects.equals(local_addr, leader)) return raft.changeMembers(name, add? InternalCommand.Type.addServer : InternalCommand.Type.removeServer); // add a unique ID to the request table, so we can correlate the response to the request int req_id=request_ids.getAndIncrement(); CompletableFuture<byte[]> future=new CompletableFuture<>(); synchronized(requests) { requests.put(req_id, future); } // we're not the current leader -> redirect request to leader and wait for response or timeout log.trace("%s: redirecting request %d to leader %s", local_addr, req_id, leader); byte[] buffer=Util.stringToBytes(name); Message redirect=new Message(leader, buffer) .putHeader(id, new RedirectHeader(add? RequestType.ADD_SERVER : RequestType.REMOVE_SERVER, req_id, false)); down_prot.down(redirect); return future; }
Example #5
Source File: LevelDBLog.java From jgroups-raft with Apache License 2.0 | 6 votes |
@Override public void truncate(int upto_index) { if ((upto_index< firstAppended) || (upto_index> lastAppended)) { //@todo wrong index, must throw runtime exception return; } WriteBatch batch=null; try { batch = db.createWriteBatch(); for (int index =firstAppended; index < upto_index; index++) { batch.delete(fromIntToByteArray(index)); } firstAppended= upto_index; batch.put(FIRSTAPPENDED, fromIntToByteArray(upto_index)); db.write(batch); } finally { Util.close(batch); } }
Example #6
Source File: AppendEntriesTest.java From jgroups-raft with Apache License 2.0 | 6 votes |
public void testRAFTPaperScenarioF() throws Exception { Address leader=Util.createRandomAddress("A"); initB(); RaftImpl impl=getImpl(b); Log log=impl.raft().log(); byte[] buf=new byte[10]; append(impl, 1, 0, new LogEntry(1, buf), leader, 1); append(impl, 2, 1, new LogEntry(1, buf), leader, 1); append(impl, 3, 1, new LogEntry(1, buf), leader, 1); append(impl, 4, 1, new LogEntry(2, buf), leader, 1); append(impl, 5, 2, new LogEntry(2, buf), leader, 1); append(impl, 6, 2, new LogEntry(2, buf), leader, 1); append(impl, 7, 2, new LogEntry(3, buf), leader, 1); append(impl, 8, 3, new LogEntry(3, buf), leader, 1); append(impl, 9, 3, new LogEntry(3, buf), leader, 1); append(impl, 10, 3, new LogEntry(3, buf), leader, 1); append(impl, 11, 3, new LogEntry(3, buf), leader, 11); AppendResult result=append(impl, 11, 6, new LogEntry(6, buf), leader, 11); assertFalse(result.isSuccess()); assertEquals(result.getIndex(), 7); assertLogIndices(log, 11, 11, 3); }
Example #7
Source File: AppendEntriesTest.java From jgroups-raft with Apache License 2.0 | 6 votes |
public void testRAFTPaperScenarioE() throws Exception { Address leader=Util.createRandomAddress("A"); initB(); RaftImpl impl=getImpl(b); Log log=impl.raft().log(); byte[] buf=new byte[10]; append(impl, 1, 0, new LogEntry(1, buf), leader, 1); append(impl, 2, 1, new LogEntry(1, buf), leader, 1); append(impl, 3, 1, new LogEntry(1, buf), leader, 1); append(impl, 4, 1, new LogEntry(4, buf), leader, 1); append(impl, 5, 4, new LogEntry(4, buf), leader, 1); append(impl, 6, 4, new LogEntry(4, buf), leader, 1); append(impl, 7, 4, new LogEntry(4, buf), leader, 7); AppendResult result=append(impl, 11, 6, new LogEntry(6, buf), leader, 7); assertFalse(result.isSuccess()); assertEquals(result.getIndex(), 7); assertLogIndices(log, 7, 7, 4); }
Example #8
Source File: AppendEntriesTest.java From jgroups-raft with Apache License 2.0 | 6 votes |
public void testRAFTPaperScenarioD() throws Exception { Address leader=Util.createRandomAddress("A"); initB(); RaftImpl impl=getImpl(b); Log log=impl.raft().log(); byte[] buf=new byte[10]; append(impl, 1, 0, new LogEntry(1, buf), leader, 1); append(impl, 2, 1, new LogEntry(1, buf), leader, 1); append(impl, 3, 1, new LogEntry(1, buf), leader, 1); append(impl, 4, 1, new LogEntry(4, buf), leader, 1); append(impl, 5, 4, new LogEntry(4, buf), leader, 1); append(impl, 6, 4, new LogEntry(5, buf), leader, 1); append(impl, 7, 5, new LogEntry(5, buf), leader, 1); append(impl, 8, 5, new LogEntry(6, buf), leader, 1); append(impl, 9, 6, new LogEntry(6, buf), leader, 1); append(impl, 10, 6, new LogEntry(6, buf), leader, 1); append(impl, 11, 6, new LogEntry(7, buf), leader, 1); append(impl, 12, 7, new LogEntry(7, buf), leader, 12); AppendResult result=append(impl, buf, leader, 10, 6, 8, 12); assertTrue(result.isSuccess()); assertEquals(result.getIndex(), 11); assertLogIndices(log, 11, 11, 8); }
Example #9
Source File: AppendEntriesTest.java From jgroups-raft with Apache License 2.0 | 6 votes |
public void testRAFTPaperScenarioC() throws Exception { Address leader=Util.createRandomAddress("A"); initB(); RaftImpl impl=getImpl(b); Log log=impl.raft().log(); byte[] buf=new byte[10]; append(impl, 1, 0, new LogEntry(1, buf), leader, 1); append(impl, 2, 1, new LogEntry(1, buf), leader, 1); append(impl, 3, 1, new LogEntry(1, buf), leader, 1); append(impl, 4, 1, new LogEntry(4, buf), leader, 1); append(impl, 5, 4, new LogEntry(4, buf), leader, 1); append(impl, 6, 4, new LogEntry(5, buf), leader, 1); append(impl, 7, 5, new LogEntry(5, buf), leader, 1); append(impl, 8, 5, new LogEntry(6, buf), leader, 1); append(impl, 9, 6, new LogEntry(6, buf), leader, 1); append(impl, 10, 6, new LogEntry(6, buf), leader, 1); append(impl, 11, 6, new LogEntry(6, buf), leader, 11); // Overwrites existing entry; does *not* advance last_applied in log AppendResult result=append(impl, 11, 6, new LogEntry(6, buf), leader, 11); assertTrue(result.isSuccess()); assertEquals(result.getIndex(), 11); assertLogIndices(log, 11, 11, 6); }
Example #10
Source File: LevelDBLog.java From jgroups-raft with Apache License 2.0 | 6 votes |
public void printMetadata() throws Exception { log.info("-----------------"); log.info("RAFT Log Metadata"); log.info("-----------------"); byte[] firstAppendedBytes = db.get(FIRSTAPPENDED); log.info("First Appended: %d", fromByteArrayToInt(firstAppendedBytes)); byte[] lastAppendedBytes = db.get(LASTAPPENDED); log.info("Last Appended: %d", fromByteArrayToInt(lastAppendedBytes)); byte[] currentTermBytes = db.get(CURRENTTERM); log.info("Current Term: %d", fromByteArrayToInt(currentTermBytes)); byte[] commitIndexBytes = db.get(COMMITINDEX); log.info("Commit Index: %d", fromByteArrayToInt(commitIndexBytes)); Address votedForTmp =Util.objectFromByteBuffer(db.get(VOTEDFOR)); log.info("Voted for: %s", votedForTmp); }
Example #11
Source File: AppendEntriesTest.java From jgroups-raft with Apache License 2.0 | 6 votes |
public void testRAFTPaperScenarioA() throws Exception { Address leader=Util.createRandomAddress("A"); initB(); RaftImpl impl=getImpl(b); Log log=impl.raft().log(); byte[] buf=new byte[10]; append(impl, 1, 0, new LogEntry(1, buf), leader, 1); append(impl, 2, 1, new LogEntry(1, buf), leader, 1); append(impl, 3, 1, new LogEntry(1, buf), leader, 1); append(impl, 4, 1, new LogEntry(4, buf), leader, 1); append(impl, 5, 4, new LogEntry(4, buf), leader, 1); append(impl, 6, 4, new LogEntry(5, buf), leader, 1); append(impl, 7, 5, new LogEntry(5, buf), leader, 1); append(impl, 8, 5, new LogEntry(6, buf), leader, 1); append(impl, 9, 6, new LogEntry(6, buf), leader, 9); AppendResult result = append(impl, 11, 6, new LogEntry(6, buf), leader, 9); assertFalse(result.isSuccess()); assertEquals(result.getIndex(), 9); assertLogIndices(log, 9, 9, 6); }
Example #12
Source File: AppendEntriesTest.java From jgroups-raft with Apache License 2.0 | 6 votes |
public void testRAFTPaperAppendOnLeader() throws Exception { Address leader=Util.createRandomAddress("A"); initB(); RaftImpl impl=getImpl(b); Log log=impl.raft().log(); byte[] buf=new byte[10]; append(impl, 1, 0, new LogEntry(1, buf), leader, 1); append(impl, 2, 1, new LogEntry(1, buf), leader, 1); append(impl, 3, 1, new LogEntry(1, buf), leader, 1); append(impl, 4, 1, new LogEntry(4, buf), leader, 1); append(impl, 5, 4, new LogEntry(4, buf), leader, 1); append(impl, 6, 4, new LogEntry(5, buf), leader, 1); append(impl, 7, 5, new LogEntry(5, buf), leader, 1); append(impl, 8, 5, new LogEntry(6, buf), leader, 1); append(impl, 9, 6, new LogEntry(6, buf), leader, 1); append(impl, 10, 6, new LogEntry(6, buf), leader, 10); AppendResult result=append(impl, 11, 6, new LogEntry(6, buf), leader, 1); assertTrue(result.isSuccess()); assertEquals(result.getIndex(), 11); assertLogIndices(log, 11, 10, 6); }
Example #13
Source File: AppendEntriesTest.java From jgroups-raft with Apache License 2.0 | 6 votes |
/** * Leader A and follower B commit 5 entries, then snapshot A. Then C comes up and should get the 5 committed entries * as well, as a snapshot */ public void testInstallSnapshotInC() throws Exception { init(true); close(true, true, c); for(int i=1; i <= 5; i++) as.put(i,i); assertSame(as, bs); // Snapshot A: as.snapshot(); // Now start C c=create("C", true); // follower cs=new ReplicatedStateMachine<>(c); c.connect(CLUSTER); Util.waitUntilAllChannelsHaveSameView(10000, 500, a, b, c); assertSame(as, bs, cs); }
Example #14
Source File: AppendEntriesTest.java From jgroups-raft with Apache License 2.0 | 6 votes |
/** * Leader A and followers B and C commit entries 1-2. Then C leaves and A and B commit entries 3-5. When C rejoins, * it should get log entries 3-5 as well. */ public void testCatchingUp() throws Exception { init(true); // A, B and C commit entries 1-2 for(int i=1; i <= 2; i++) as.put(i,i); assertSame(as, bs, cs); // Now C leaves close(true, true, c); // A and B commit entries 3-5 for(int i=3; i <= 5; i++) as.put(i,i); assertSame(as, bs); // Now start C again: entries 1-5 will have to get resent to C as its log was deleted above (otherwise only 3-5 // would have to be resent) c=create("C", true); // follower cs=new ReplicatedStateMachine<>(c); c.connect(CLUSTER); Util.waitUntilAllChannelsHaveSameView(10000, 500, a,b,c); // Now C should also have the same entries (1-5) as A and B assertSame(as, bs, cs); }
Example #15
Source File: ReplicatedStateMachine.java From jgroups-raft with Apache License 2.0 | 6 votes |
protected V invoke(byte command, K key, V val, boolean ignore_return_value) throws Exception { ByteArrayDataOutputStream out=new ByteArrayDataOutputStream(256); try { out.writeByte(command); Util.objectToStream(key, out); if(val != null) Util.objectToStream(val, out); } catch(Exception ex) { throw new Exception("serialization failure (key=" + key + ", val=" + val + ")", ex); } byte[] buf=out.buffer(); byte[] rsp=raft.set(buf, 0, out.position(), repl_timeout, TimeUnit.MILLISECONDS); return ignore_return_value? null: (V)Util.objectFromByteBuffer(rsp); }
Example #16
Source File: CLIENT.java From jgroups-raft with Apache License 2.0 | 6 votes |
public void accept(byte[] buf, Throwable ex) { try { if(ex != null) { byte[] rsp_buffer=Util.objectToByteBuffer(ex); send(output, RequestType.rsp, rsp_buffer, 0, rsp_buffer.length); return; } if(buf == null) buf=BUF; send(output, RequestType.rsp, buf, 0, buf.length); } catch(Throwable t) { log.error("failed in sending response to client", t); } finally { Util.close(output,input,s); } }
Example #17
Source File: ReplicatedStateMachine.java From jgroups-raft with Apache License 2.0 | 6 votes |
@Override public byte[] apply(byte[] data, int offset, int length) throws Exception { ByteArrayDataInputStream in=new ByteArrayDataInputStream(data, offset, length); byte command=in.readByte(); switch(command) { case PUT: K key=Util.objectFromStream(in); V val=Util.objectFromStream(in); V old_val; synchronized(map) { old_val=map.put(key, val); } notifyPut(key, val, old_val); return old_val == null? null : Util.objectToByteBuffer(old_val); case REMOVE: key=Util.objectFromStream(in); synchronized(map) { old_val=map.remove(key); } notifyRemove(key, old_val); return old_val == null? null : Util.objectToByteBuffer(old_val); default: throw new IllegalArgumentException("command " + command + " is unknown"); } }
Example #18
Source File: CompletableFutureTest.java From jgroups-raft with Apache License 2.0 | 5 votes |
public void run() { Util.sleep(sleep); if(t != null) future.completeExceptionally(t); else future.complete(result); }
Example #19
Source File: LogTest.java From jgroups-raft with Apache License 2.0 | 5 votes |
public void testMetadataInAReopenedLog(Log log) throws Exception { this.log=log; log.init(filename, null); byte[] buf=new byte[10]; append(log, 1, false, buf, 1,1,1, 4,4, 5,5, 6,6,6); log.commitIndex(10); log.votedFor(Util.createRandomAddress("A")); log.close(); log.init(filename, null); assertIndices(0, 10, 10, 6); assertEquals(log.votedFor().toString(), Util.createRandomAddress("A").toString()); }
Example #20
Source File: LogTest.java From jgroups-raft with Apache License 2.0 | 5 votes |
public void testFields(Log log) throws Exception { Address addr=Util.createRandomAddress("A"); this.log=log; log.init(filename, null); log.currentTerm(22); int current_term=log.currentTerm(); assertEquals(current_term, 22); log.votedFor(addr); Address voted_for=log.votedFor(); assertEquals(addr, voted_for); log.close(); log.init(filename, null); current_term=log.currentTerm(); assertEquals(current_term, 22); voted_for=log.votedFor(); assertEquals(addr, voted_for); log.close(); log.delete(); log.init(filename, null); current_term=log.currentTerm(); assertEquals(current_term, 0); voted_for=log.votedFor(); assertNull(voted_for); }
Example #21
Source File: PropsToAsciidoc.java From jgroups-raft with Apache License 2.0 | 5 votes |
public static void main(String[] args) { if (args.length != 1) { help(); System.err.println("args[0]=" + args[0]); return; } String prot_file = args[0]; String temp_file = prot_file + ".tmp"; try { // first copy protocols.adoc file into protocols.adoc.xml File f = new File(temp_file); copy(new FileReader(new File(prot_file)), new FileWriter(f)); String s = fileToString(f); Set<Class<Protocol>> classes = Util.findClassesAssignableFrom("org.jgroups.protocols.raft", Protocol.class); // classes.addAll(Util.findClassesAssignableFrom("org.jgroups.protocols.pbcast",Protocol.class)); Properties props = new Properties(); for (Class<Protocol> clazz : classes) convertProtocolToAsciidocTable(props,clazz); String result = Util.substituteVariable(s, props); FileWriter fw = new FileWriter(f, false); fw.write(result); fw.flush(); fw.close(); } catch (Exception e) { e.printStackTrace(); } }
Example #22
Source File: VoteTest.java From jgroups-raft with Apache License 2.0 | 5 votes |
protected static JChannel create(String name, List<String> mbrs) throws Exception { RAFT raft=new RAFT().members(mbrs).raftId(name).stateMachine(new DummyStateMachine()) .logClass("org.jgroups.protocols.raft.InMemoryLog").logName(name + "-" + CLUSTER); JChannel ch=new JChannel(Util.getTestStack(new ELECTION(), raft, new REDIRECT())).name(name); ch.connect(CLUSTER); return ch; }
Example #23
Source File: Client.java From jgroups-raft with Apache License 2.0 | 5 votes |
protected static void start(InetAddress dest, int port, String add_server, String remove_server) throws Throwable { try(Socket sock=new Socket(dest, port); DataInputStream in=new DataInputStream(sock.getInputStream()); DataOutputStream out=new DataOutputStream(sock.getOutputStream())) { CLIENT.RequestType type=add_server != null? CLIENT.RequestType.add_server : CLIENT.RequestType.remove_server; out.writeByte((byte)type.ordinal()); byte[] buf=Util.stringToBytes(add_server != null? add_server : remove_server); out.writeInt(buf.length); out.write(buf, 0, buf.length); type=CLIENT.RequestType.values()[in.readByte()]; if(type != CLIENT.RequestType.rsp) throw new IllegalStateException(String.format("expected type %s but got %s", CLIENT.RequestType.rsp, type)); int len=in.readInt(); if(len == 0) return; buf=new byte[len]; in.readFully(buf); Object response=Util.objectFromByteBuffer(buf); if(response instanceof Throwable) throw (Throwable)response; System.out.println("response = " + response); } catch(Exception ex) { ex.printStackTrace(); } }
Example #24
Source File: DynamicMembershipTest.java From jgroups-raft with Apache License 2.0 | 5 votes |
protected void close(boolean remove_log, boolean remove_snapshot, JChannel ... channels) { for(JChannel ch: channels) { if(ch == null) continue; RAFT raft=ch.getProtocolStack().findProtocol(RAFT.class); if(remove_log) raft.log().delete(); // remove log files after the run if(remove_snapshot) raft.deleteSnapshot(); Util.close(ch); } }
Example #25
Source File: ReplicatedStateMachine.java From jgroups-raft with Apache License 2.0 | 5 votes |
@Override public void writeContentTo(DataOutput out) throws Exception { synchronized(map) { int size=map.size(); Bits.writeInt(size, out); for(Map.Entry<K,V> entry : map.entrySet()) { Util.objectToStream(entry.getKey(), out); Util.objectToStream(entry.getValue(), out); } } }
Example #26
Source File: ReplicatedStateMachine.java From jgroups-raft with Apache License 2.0 | 5 votes |
@Override public void readContentFrom(DataInput in) throws Exception { int size=Bits.readInt(in); Map<K,V> tmp=new HashMap<>(size); for(int i=0; i < size; i++) { K key=Util.objectFromStream(in); V val=Util.objectFromStream(in); tmp.put(key, val); } synchronized(map) { map.putAll(tmp); } }
Example #27
Source File: ElectionsTest.java From jgroups-raft with Apache License 2.0 | 5 votes |
protected static JChannel create(String name, Supplier<RAFT> raftSupplier) throws Exception { ELECTION election=new ELECTION().noElections(true); RAFT raft=raftSupplier.get().members(members).raftId(name) .logClass("org.jgroups.protocols.raft.InMemoryLog").logName(name + "-" + CLUSTER); REDIRECT client=new REDIRECT(); return new JChannel(Util.getTestStack(election, raft, client)).name(name); }
Example #28
Source File: ReplicatedStateMachineDemo.java From jgroups-raft with Apache License 2.0 | 5 votes |
protected static String read(String name) { try { return Util.readStringFromStdin(name + ": "); } catch(Exception e) { return null; } }
Example #29
Source File: JGroupsMessenger.java From tutorials with MIT License | 5 votes |
@Override public void setState(InputStream input) { // NOTE: since we know that incrementing the count and transferring the state // is done inside the JChannel's thread, we don't have to worry about synchronizing // messageCount. For production code it should be synchronized! try { // Deserialize messageCount = Util.objectFromStream(new DataInputStream(input)); } catch (Exception e) { System.out.println("Error deserialing state!"); } System.out.println(messageCount + " is the current messagecount."); }
Example #30
Source File: ElectionsTest.java From jgroups-raft with Apache License 2.0 | 5 votes |
protected static void close(boolean remove_log, boolean remove_snapshot, JChannel ch) { if(ch == null) return; RAFT raft=ch.getProtocolStack().findProtocol(RAFT.class); if(remove_log) raft.log().delete(); // remove log files after the run if(remove_snapshot) raft.deleteSnapshot(); Util.close(ch); }