@Override public void send(final Message msg) throws Exception { Runnable async_send=() -> { ByteArrayDataOutputStream out=new ByteArrayDataOutputStream((int)(msg.size() + 10)); sendSingleMessage(msg, out); }; thread_pool.execute(async_send); } }
protected static long sizeOfAllMessages(Table<Message> buf, boolean include_headers) { return buf.stream().reduce(0L, (size,el) -> { if(el == null) return size; else return size + (include_headers? el.size() : el.getLength()); }, (l,r) -> l); }
public Object down(Message msg) { long size=msg.size(); num_sent_msgs++; if(size > frag_size) { if(log.isTraceEnabled()) { StringBuilder sb=new StringBuilder("message size is "); sb.append(size).append(", will fragment (frag_size=").append(frag_size).append(')'); log.trace(sb.toString()); } fragment(msg, size); // Fragment and pass down return null; } return down_prot.down(msg); }
public void send(Message msg) throws Exception { ByteArrayDataOutputStream out=new ByteArrayDataOutputStream((int)(msg.size() + 10)); sendSingleMessage(msg, out); }
public void send(Message msg) throws Exception { num_senders.incrementAndGet(); long size=msg.size(); lock.lock(); try { if(count + size >= transport.getMaxBundleSize()) sendBundledMessages(); addMessage(msg, size); // at this point, we haven't sent our message yet ! if(num_senders.decrementAndGet() == 0) // no other sender threads present at this time sendBundledMessages(); // else there are other sender threads waiting, so our message will be sent by a different thread } finally { lock.unlock(); } } }
public void send(Message msg) throws Exception { if(msg == null) throw new IllegalArgumentException("message must not be null"); num_threads.incrementAndGet(); int tmp_write_index=getWriteIndex(read_index.get()); // System.out.printf("[%d] tmp_write_index=%d\n", Thread.currentThread().getId(), tmp_write_index); if(tmp_write_index == -1) { log.warn("buf is full: %s\n", toString()); unparkIfNeeded(0); return; } buf[tmp_write_index]=msg; unparkIfNeeded(msg.size()); }
public Object up(Message msg) { if(log.isTraceEnabled()) { long size=raw_buffer? msg.getLength() : msg.size(); if(size >= min_size) { StringBuilder sb=new StringBuilder(local_addr + ".up(): size of message buffer="); sb.append(Util.printBytes(size)).append(", " + numHeaders(msg) + " headers"); if(print_msg) sb.append(", headers=" + msg.printHeaders()); log.trace(sb); } } return up_prot.up(msg); }
public Object down(Message msg) { if(log.isTraceEnabled()) { long size=raw_buffer? msg.getLength() : msg.size(); if(size >= min_size) { StringBuilder sb=new StringBuilder(local_addr + ".down(): size of message buffer="); sb.append(Util.printBytes(size)).append(", " + numHeaders(msg) + " headers"); if(print_msg) sb.append(", headers=" + msg.printHeaders()); log.trace(sb); } } return down_prot.down(msg); }
protected void _send(Message msg, Address dest) { try { send(msg, dest); } catch(InterruptedIOException iex) { } catch(InterruptedException interruptedEx) { Thread.currentThread().interrupt(); // let someone else handle the interrupt } catch(SocketException sock_ex) { log.trace(Util.getMessage("SendFailure"), local_addr, (dest == null? "cluster" : dest), msg.size(), sock_ex.toString(), msg.printHeaders()); } catch(Throwable e) { log.error(Util.getMessage("SendFailure"), local_addr, (dest == null? "cluster" : dest), msg.size(), e.toString(), msg.printHeaders()); } }
private void processForUnicast(Message msg, int direction) { Object o = msg.getHeader(unicastHeaderId); if (o instanceof UNICAST3.Header && stats != null) { UNICAST3.Header hdr = (UNICAST3.Header) o; switch (direction) { case INCOMING: stats.incUcastReadBytes((int) msg.size()); break; case OUTGOING: stats.incUcastWriteBytes((int) msg.size()); switch (hdr.type()) { case UNICAST3.Header.XMIT_REQ: stats.incUcastRetransmits(); break; } break; } } }
public static Buffer messageToByteBuffer(Message msg) throws Exception { ByteArrayDataOutputStream out=new ByteArrayDataOutputStream((int)msg.size()+1); out.writeBoolean(msg != null); if(msg != null) msg.writeTo(out); return out.getBuffer(); }
protected int marshalMessagesToSameDestination(Address dest, Message[] buf, final int start_index, final int end_index, int max_bundle_size) throws Exception { int num_msgs=0, bytes=0; for(int i=start_index; i != end_index; i=increment(i)) { Message msg=buf[i]; if(msg != null && msg != NULL_MSG && Objects.equals(dest, msg.dest())) { long msg_size=msg.size(); if(bytes + msg_size > max_bundle_size) break; bytes+=msg_size; num_msgs++; buf[i]=NULL_MSG; msg.writeToNoAddrs(msg.src(), output, transport.getId()); } } return num_msgs; }
public void run() { try { int drained=rb.drainToBlocking(remove_queue); if(drained == 1) { output.position(0); sendSingleMessage(remove_queue[0]); return; } for(int i=0; i < drained; i++) { Message msg=remove_queue[i]; long size=msg.size(); if(count + size >= transport.getMaxBundleSize()) sendBundledMessages(); addMessage(msg, msg.size()); } sendBundledMessages(); } catch(Throwable t) { } }
protected int marshalMessagesToSameDestination(Address dest, Message[] buf, int start_index, int available_msgs, int max_bundle_size) throws Exception { int num_msgs=0, bytes=0; while(available_msgs > 0) { Message msg=buf[start_index]; if(msg != null && Objects.equals(dest, msg.dest())) { long msg_size=msg.size(); if(bytes + msg_size > max_bundle_size) break; bytes+=msg_size; num_msgs++; buf[start_index]=null; msg.writeToNoAddrs(msg.src(), output, transport.getId()); } available_msgs--; start_index=increment(start_index); } return num_msgs; }
protected int marshalMessagesToSameDestination(Address dest, Message[] buf, int start_index, final int end_index, int max_bundle_size) throws Exception { int num_msgs=0, bytes=0; for(;;) { Message msg=buf[start_index]; if(msg != null && Objects.equals(dest, msg.dest())) { long size=msg.size(); if(bytes + size > max_bundle_size) break; bytes+=size; num_msgs++; buf[start_index]=null; msg.writeToNoAddrs(msg.src(), output, transport.getId()); } if(start_index == end_index) break; start_index=advance(start_index); } return num_msgs; }
@Override protected void sendMcastDiscoveryRequest(Message msg) { try { if(msg.getSrc() == null) msg.setSrc(local_addr); ByteArrayDataOutputStream out=new ByteArrayDataOutputStream((int)msg.size()); msg.writeTo(out); for(int i=bind_port; i <= bind_port+port_range; i++) { DatagramPacket packet=new DatagramPacket(out.buffer(), 0, out.position(), dest_addr, i); sock.send(packet); } } catch(Exception ex) { log.error(Util.getMessage("FailedSendingDiscoveryRequest"), ex); } }
protected void sendSingleMessage(final Message msg) { Address dest=msg.getDest(); try { Util.writeMessage(msg, output, dest == null); transport.doSend(output.buffer(), 0, output.position(), dest); if(transport.statsEnabled()) transport.incrNumSingleMsgsSent(1); } catch(SocketException | SocketTimeoutException sock_ex) { log.trace(Util.getMessage("SendFailure"), transport.localAddress(), (dest == null? "cluster" : dest), msg.size(), sock_ex.toString(), msg.printHeaders()); } catch(Throwable e) { log.error(Util.getMessage("SendFailure"), transport.localAddress(), (dest == null? "cluster" : dest), msg.size(), e.toString(), msg.printHeaders()); } }
protected void sendSingleMessage(final Message msg, final ByteArrayDataOutputStream output) { Address dest=msg.getDest(); try { output.position(0); Util.writeMessage(msg, output, dest == null); transport.doSend(output.buffer(), 0, output.position(), dest); if(transport.statsEnabled()) transport.incrNumSingleMsgsSent(1); } catch(SocketException | SocketTimeoutException sock_ex) { log.trace(Util.getMessage("SendFailure"), transport.localAddress(), (dest == null? "cluster" : dest), msg.size(), sock_ex.toString(), msg.printHeaders()); } catch(Throwable e) { log.error(Util.getMessage("SendFailure"), transport.localAddress(), (dest == null? "cluster" : dest), msg.size(), e.toString(), msg.printHeaders()); } }
@Test public void recorderHandlesRejectedExecution() throws Exception { Message msg = mock(Message.class); when(msg.getHeader(any(Short.class))).thenReturn(Header.createDataHeader(1L, (short) 1, true)); when(msg.size()).thenReturn(150L); // GEODE-1178, the TP protocol may throw a RejectedExecutionException & StatRecorder should // retry when(mockDownProtocol.down(any(Event.class))).thenThrow(new RejectedExecutionException()); // after the first down() throws an exception we want StatRecorder to retry, so // we set the Manager to say no shutdown is in progress the first time and then say // one IS in progress so we can break out of the StatRecorder exception handling loop when(services.getCancelCriterion()).thenReturn(new Services().getCancelCriterion()); Manager manager = mock(Manager.class); when(services.getManager()).thenReturn(manager); when(manager.shutdownInProgress()).thenReturn(Boolean.FALSE, Boolean.TRUE); verify(mockDownProtocol, never()).down(isA(Event.class)); Event evt = new Event(Event.MSG, msg); recorder.down(evt); verify(mockDownProtocol, times(2)).down(isA(Event.class)); }
protected Object _down(Message msg) throws Exception { Address dest=msg.dest(); setSourceAddress(msg); // very important !! listToBuffer() will fail with a null src address !! int size=(int)msg.size(); ByteArrayDataOutputStream out=new ByteArrayDataOutputStream(size + Global.INT_SIZE); out.writeInt(size); msg.writeTo(out); if(dest != null) // unicast sendTo(dest, out.buffer(), 0, out.position()); else { // multicast Collection<Address> dests=view != null? view.getMembers(): addr_table.keySet(); for(Address dst: dests) { try { sendTo(dst, out.buffer(), 0, out.position()); } catch(Throwable t) { log.error("failed sending multicast message to " + dst, t); } } } return null; }