public Connection(Socket sock) throws Exception { this.sock=sock; peer_addr=new IpAddress((InetSocketAddress)sock.getRemoteSocketAddress()); in=new DataInputStream(createBufferedInputStream(sock.getInputStream())); out=new DataOutputStream(createBufferedOutputStream(sock.getOutputStream())); runner=new Runner(new DefaultThreadFactory("tcp", false, true), "conn-" + sock.getLocalPort(), this, null); }
public Thread getThread() {return bundler_thread.getThread();} public int size() {return rb.size();}
@ManagedAttribute public boolean isRequestHandlerRunning() {return req_handler.isRunning();} @ManagedAttribute public int requestQueueSize() {return req_queue.size();}
public void stop() { bundler_thread.stop(); }
protected Connection start() { runner.start(); return this; }
protected synchronized void becomeKeyserver() throws Exception { if(srv_sock == null || srv_sock.isClosed()) { log.debug("%s: becoming keyserver; creating server socket", local_addr); srv_sock=createServerSocket(); srv_sock_handler=new Runner(getThreadFactory(), SSL_KEY_EXCHANGE.class.getSimpleName() + "-runner", this::accept, () -> Util.close(srv_sock)); srv_sock_handler.start(); log.debug("SSL server socket listening on %s", srv_sock.getLocalSocketAddress()); } }
@Override public void handleView(View v) { Address old_coord=this.view != null? this.view.getCoord() : null; super.handleView(v); if(v.size() > 0) { coord=v.getCoord(); log.debug("%s: coord=%s, is_coord=%b", local_addr, coord, isCoord()); } if(Objects.equals(local_addr, coord)) { if(v instanceof MergeView || !Objects.equals(local_addr, old_coord)) { // I'm the new coord: run reconciliation to find all existing locks (and pending lock/unlock requests) runReconciliation(); req_handler.start(); } } else { if(Objects.equals(local_addr, old_coord)) { log.debug("%s: not coordinator anymore; stopping the request handler", local_addr); req_handler.stop(); // clears the req-queue server_locks.clear(); } } }
public void stop() { bundler_thread.stop(); }
public void start() { bundler_thread.start(); }
protected synchronized void becomeKeyserver() throws Exception { if(srv_sock == null || srv_sock.isClosed()) { log.debug("%s: becoming keyserver; creating server socket", local_addr); srv_sock=createServerSocket(); srv_sock_handler=new Runner(getThreadFactory(), SSL_KEY_EXCHANGE.class.getSimpleName() + "-runner", this::accept, () -> Util.close(srv_sock)); srv_sock_handler.start(); log.debug("SSL server socket listening on %s", srv_sock.getLocalSocketAddress()); } }
@Override public void handleView(View v) { Address old_coord=this.view != null? this.view.getCoord() : null; super.handleView(v); if(v.size() > 0) { coord=v.getCoord(); log.debug("%s: coord=%s, is_coord=%b", local_addr, coord, isCoord()); } if(Objects.equals(local_addr, coord)) { if(v instanceof MergeView || !Objects.equals(local_addr, old_coord)) { // I'm the new coord: run reconciliation to find all existing locks (and pending lock/unlock requests) runReconciliation(); req_handler.start(); } } else { if(Objects.equals(local_addr, old_coord)) { log.debug("%s: not coordinator anymore; stopping the request handler", local_addr); req_handler.stop(); // clears the req-queue server_locks.clear(); } } }
public void stop() { bundler_thread.stop(); }
public Acceptor(InetAddress bind, int port) throws Exception { this.bind=bind; this.port=port; runner=new Runner(new DefaultThreadFactory("tcp", true, true), "acceptor", this, null); }
public void start() { bundler_thread.start(); }
protected void unparkIfNeeded(long size) { long acc_bytes=size > 0? accumulated_bytes.addAndGet(size) : accumulated_bytes.get(); boolean size_exceeded=acc_bytes >= transport.getMaxBundleSize() && accumulated_bytes.compareAndSet(acc_bytes, 0); boolean no_other_threads=num_threads.decrementAndGet() == 0; boolean unpark=size_exceeded || no_other_threads; // only 2 threads at a time should do this in parallel (1st cond and 2nd cond) if(unpark && unparking.compareAndSet(false, true)) { Thread thread=bundler_thread.getThread(); if(thread != null) LockSupport.unpark(thread); unparking.set(false); } }
@ManagedAttribute public boolean isRequestHandlerRunning() {return req_handler.isRunning();} @ManagedAttribute public int requestQueueSize() {return req_queue.size();}
protected void stop() { runner.stop(); }
public void init(TP transport) { super.init(transport); bundler_thread=new Runner(transport.getThreadFactory(), THREAD_NAME, run_function, this::reset); }
public void start() { bundler_thread.start(); }
public void send(Message msg) throws Exception { if(msg == null) throw new IllegalArgumentException("message must not be null"); num_threads.incrementAndGet(); int tmp_index=getWriteIndex(); // decrements write_permits // System.out.printf("[%d] tmp_index=%d\n", Thread.currentThread().getId(), tmp_index); if(tmp_index == -1) { log.warn("buf is full (num_permits: %d, bundler: %s)\n", write_permits.get(), toString()); num_threads.decrementAndGet(); return; } buf[tmp_index]=msg; long acc_bytes=accumulated_bytes.addAndGet(msg.size()); int current_threads=num_threads.decrementAndGet(); boolean no_other_threads=current_threads == 0; boolean unpark=(acc_bytes >= transport.getMaxBundleSize() && accumulated_bytes.compareAndSet(acc_bytes, 0)) || no_other_threads; // only 2 threads at a time should do this (1st cond and 2nd cond), so we have to reduce this to // 1 thread as advanceWriteIndex() is not thread safe if(unpark && unparking.compareAndSet(false, true)) { int num_advanced=advanceWriteIndex(); size.addAndGet(num_advanced); if(num_advanced > 0) { Thread thread=bundler_thread.getThread(); if(thread != null) LockSupport.unpark(thread); } unparking.set(false); } }