protected static final Request requestFromBuffer(byte[] buf, int offset, int length) throws Exception { ByteArrayInputStream input=new ByteArrayInputStream(buf, offset, length); DataInputStream in=new DataInputStream(input); RequestType type=RequestType.values()[in.readByte()]; Request retval=createRequest(type); retval.readFrom(in); return retval; }
protected static final Response responseFromBuffer(byte[] buf, int offset, int length) throws Exception { ByteArrayInputStream input=new ByteArrayInputStream(buf, offset, length); DataInputStream in=new DataInputStream(input); ResponseType type=ResponseType.values()[in.readByte()]; Response retval=createResponse(type); retval.readFrom(in); return retval; }
/** * Deletes a counter instance (on the coordinator) * @param name The name of the counter. No-op if the counter doesn't exist */ public void deleteCounter(String name) { counter_prot.deleteCounter(name); }
/** Sent asynchronously - we don't wait for an ack */ public void deleteCounter(String name) { Owner owner=getOwner(); Request req=new DeleteRequest(owner, name); sendRequest(coord, req); if(!local_addr.equals(coord)) counters.remove(name); }
@Override public void set(long new_value) { if(local_addr.equals(coord)) { VersionedValue val=getCounter(name); val.set(new_value); if(backup_coords != null) updateBackups(name, val.value, val.version); return; } Owner owner=getOwner(); Request req=new SetRequest(owner, name, new_value); Promise<long[]> promise=new Promise<>(); pending_requests.put(owner, new Tuple<>(req, promise)); sendRequest(coord, req); Object obj=null; try { obj=promise.getResultWithTimeout(timeout); if(obj instanceof Throwable) throw new IllegalStateException((Throwable)obj); long[] result=(long[])obj; long value=result[0], version=result[1]; if(!coord.equals(local_addr)) counters.put(name, new VersionedValue(value, version)); } catch(TimeoutException e) { throw new RuntimeException(e); } }
public Object up(Message msg) { CounterHeader hdr=msg.getHeader(id); if(hdr == null) return up_prot.up(msg); try { Object obj=streamableFromBuffer(msg.getRawBuffer(), msg.getOffset(), msg.getLength()); if(log.isTraceEnabled()) log.trace("[" + local_addr + "] <-- [" + msg.getSrc() + "] " + obj); if(obj instanceof Request) { handleRequest((Request)obj, msg.getSrc()); } else if(obj instanceof Response) { handleResponse((Response)obj, msg.getSrc()); } else { log.error(Util.getMessage("ReceivedObjectIsNeitherARequestNorAResponse") + obj); } } catch(Exception ex) { log.error(Util.getMessage("FailedHandlingMessage"), ex); } return null; }
/** * Returns an existing counter, or creates a new one if none exists * @param name Name of the counter, different counters have to have different names * @param initial_value The initial value of a new counter if there is no existing counter. Ignored * if the counter already exists * @return The counter implementation */ public Counter getOrCreateCounter(String name, long initial_value) { return counter_prot.getOrCreateCounter(name, initial_value); }
protected synchronized void startReconciliationTask() { if(reconciliation_task_future == null || reconciliation_task_future.isDone()) { reconciliation_task=new ReconciliationTask(); reconciliation_task_future=getTransport().getTimer().schedule(reconciliation_task, 0, TimeUnit.MILLISECONDS); } }
public String dumpPendingRequests() {return counter_prot.dumpPendingRequests();}
if(log.isTraceEnabled() && ((ReconcileResponse)rsp).names != null && ((ReconcileResponse)rsp).names.length > 0) log.trace("[" + local_addr + "] <-- [" + sender + "] RECONCILE-RSP: " + dump(((ReconcileResponse)rsp).names, ((ReconcileResponse)rsp).values, ((ReconcileResponse)rsp).versions)); if(reconciliation_task != null) reconciliation_task.add((ReconcileResponse)rsp, sender);
@Override public long addAndGet(long delta) { if(local_addr.equals(coord)) { VersionedValue val=getCounter(name); long retval=val.addAndGet(delta)[0]; if(backup_coords != null) updateBackups(name, val.value, val.version); return retval; } Owner owner=getOwner(); Request req=new AddAndGetRequest(owner, name, delta); Promise<long[]> promise=new Promise<>(); pending_requests.put(owner, new Tuple<>(req, promise)); sendRequest(coord, req); Object obj=null; try { obj=promise.getResultWithTimeout(timeout); if(obj instanceof Throwable) throw new IllegalStateException((Throwable)obj); long[] result=(long[])obj; long value=result[0], version=result[1]; if(!coord.equals(local_addr)) counters.put(name, new VersionedValue(value, version)); return value; } catch(TimeoutException e) { throw new RuntimeException(e); } }
public Object up(Message msg) { CounterHeader hdr=msg.getHeader(id); if(hdr == null) return up_prot.up(msg); try { Object obj=streamableFromBuffer(msg.getRawBuffer(), msg.getOffset(), msg.getLength()); if(log.isTraceEnabled()) log.trace("[" + local_addr + "] <-- [" + msg.getSrc() + "] " + obj); if(obj instanceof Request) { handleRequest((Request)obj, msg.getSrc()); } else if(obj instanceof Response) { handleResponse((Response)obj, msg.getSrc()); } else { log.error(Util.getMessage("ReceivedObjectIsNeitherARequestNorAResponse") + obj); } } catch(Exception ex) { log.error(Util.getMessage("FailedHandlingMessage"), ex); } return null; }
public Counter getOrCreateCounter(String name, long initial_value) { if(local_addr == null) throw new IllegalArgumentException("the channel needs to be connected before creating or getting a counter"); Owner owner=getOwner(); GetOrCreateRequest req=new GetOrCreateRequest(owner, name, initial_value); Promise<long[]> promise=new Promise<>(); pending_requests.put(owner, new Tuple<>(req, promise)); sendRequest(coord, req); long[] result=new long[0]; try { result=promise.getResultWithTimeout(timeout); long value=result[0], version=result[1]; if(!coord.equals(local_addr)) counters.put(name, new VersionedValue(value, version)); return new CounterImpl(name); } catch(TimeoutException e) { throw new RuntimeException(e); } }
/** * Returns an existing counter, or creates a new one if none exists * @param name Name of the counter, different counters have to have different names * @param initial_value The initial value of a new counter if there is no existing counter. Ignored * if the counter already exists * @return The counter implementation */ public Counter getOrCreateCounter(String name, long initial_value) { return counter_prot.getOrCreateCounter(name, initial_value); }
protected synchronized void startReconciliationTask() { if(reconciliation_task_future == null || reconciliation_task_future.isDone()) { reconciliation_task=new ReconciliationTask(); reconciliation_task_future=getTransport().getTimer().schedule(reconciliation_task, 0, TimeUnit.MILLISECONDS); } }
public String dumpPendingRequests() {return counter_prot.dumpPendingRequests();}
if(log.isTraceEnabled() && ((ReconcileResponse)rsp).names != null && ((ReconcileResponse)rsp).names.length > 0) log.trace("[" + local_addr + "] <-- [" + sender + "] RECONCILE-RSP: " + dump(((ReconcileResponse)rsp).names, ((ReconcileResponse)rsp).values, ((ReconcileResponse)rsp).versions)); if(reconciliation_task != null) reconciliation_task.add((ReconcileResponse)rsp, sender);
@Override public boolean compareAndSet(long expect, long update) { if(local_addr.equals(coord)) { VersionedValue val=getCounter(name); boolean retval=val.compareAndSet(expect, update) != null; if(backup_coords != null) updateBackups(name, val.value, val.version); return retval; Owner owner=getOwner(); Request req=new CompareAndSetRequest(owner, name, expect, update); Promise<long[]> promise=new Promise<>(); pending_requests.put(owner, new Tuple<>(req, promise)); sendRequest(coord, req); Object obj=null; try {
/** Sent asynchronously - we don't wait for an ack */ public void deleteCounter(String name) { Owner owner=getOwner(); Request req=new DeleteRequest(owner, name); sendRequest(coord, req); if(!local_addr.equals(coord)) counters.remove(name); }
protected static final Request requestFromBuffer(byte[] buf, int offset, int length) throws Exception { ByteArrayInputStream input=new ByteArrayInputStream(buf, offset, length); DataInputStream in=new DataInputStream(input); RequestType type=RequestType.values()[in.readByte()]; Request retval=createRequest(type); retval.readFrom(in); return retval; }