@Override public void returnOutputBuffer(BufferObjectDataOutput out) { if (out == null) { return; } out.clear(); offerOrClose(outputQueue, out); }
DefaultPortableWriter(PortableSerializer serializer, BufferObjectDataOutput out, ClassDefinition cd) throws IOException { this.serializer = serializer; this.out = out; this.cd = cd; this.writtenFields = createHashSet(cd.getFieldCount()); this.begin = out.position(); // room for final offset out.writeZeroBytes(4); out.writeInt(cd.getFieldCount()); this.offset = out.position(); // one additional for raw data int fieldIndexesLength = (cd.getFieldCount() + 1) * INT_SIZE_IN_BYTES; out.writeZeroBytes(fieldIndexesLength); }
public static byte[] createStreamPacketHeader(NodeEngine nodeEngine, long executionId, int destinationVertexId, int ordinal) { try (BufferObjectDataOutput out = createObjectDataOutput(nodeEngine)) { out.writeLong(executionId); out.writeInt(destinationVertexId); out.writeInt(ordinal); return out.toByteArray(); } catch (IOException e) { throw sneakyThrow(e); } }
@Override public void writeNullPortable(String fieldName, int factoryId, int classId) throws IOException { setPosition(fieldName, FieldType.PORTABLE); out.writeBoolean(true); out.writeInt(factoryId); out.writeInt(classId); }
private FieldDefinition setPosition(String fieldName, FieldType fieldType) throws IOException { if (raw) { throw new HazelcastSerializationException("Cannot write Portable fields after getRawDataOutput() is called!"); } FieldDefinition fd = cd.getField(fieldName); if (fd == null) { throw new HazelcastSerializationException("Invalid field name: '" + fieldName + "' for ClassDefinition {id: " + cd.getClassId() + ", version: " + cd.getVersion() + "}"); } if (writtenFields.add(fieldName)) { int pos = out.position(); int index = fd.getIndex(); out.writeInt(offset + index * INT_SIZE_IN_BYTES, pos); out.writeShort(fieldName.length()); out.writeBytes(fieldName); out.writeByte(fieldType.getId()); } else { throw new HazelcastSerializationException("Field '" + fieldName + "' has already been written!"); } return fd; }
public void send(JoinMessage joinMessage) { if (!running) { return; } final BufferObjectDataOutput out = sendOutput; synchronized (sendLock) { try { out.writeByte(Packet.VERSION); out.writeObject(joinMessage); byte[] processed = outputProcessor != null ? outputProcessor.process(out.toByteArray()) : out.toByteArray(); datagramPacketSend.setData(processed); multicastSocket.send(datagramPacketSend); out.clear(); } catch (IOException e) { logger.warning("You probably have too long Hazelcast configuration!", e); } } } }
private byte[] toBytes(Object obj, int leftPadding, boolean writeHash, PartitioningStrategy strategy, ByteOrder serializerTypeIdByteOrder) { checkNotNull(obj); checkNotNull(serializerTypeIdByteOrder); BufferPool pool = bufferPoolThreadLocal.get(); BufferObjectDataOutput out = pool.takeOutputBuffer(); try { out.position(leftPadding); SerializerAdapter serializer = serializerFor(obj); if (writeHash) { int partitionHash = calculatePartitionHash(obj, strategy); out.writeInt(partitionHash, BIG_ENDIAN); } out.writeInt(serializer.getTypeId(), serializerTypeIdByteOrder); serializer.write(out, obj); return out.toByteArray(); } catch (Throwable e) { throw handleSerializeException(obj, e); } finally { pool.returnOutputBuffer(out); } }
private boolean tryFillOutputBuffer() { try { // header size + slot for writtenCount outputBuffer.position(bufPosPastHeader + Bits.INT_SIZE_IN_BYTES); int writtenCount = 0; for (Object item; outputBuffer.position() < packetSizeLimit && isWithinLimit(sentSeq, sendSeqLimitCompressed) && (item = inbox.poll()) != null; writtenCount++ ) { ObjectWithPartitionId itemWithPId = item instanceof ObjectWithPartitionId ? (ObjectWithPartitionId) item : new ObjectWithPartitionId(item, - 1); final int mark = outputBuffer.position(); outputBuffer.writeObject(itemWithPId.getItem()); sentSeq += estimatedMemoryFootprint(outputBuffer.position() - mark); outputBuffer.writeInt(itemWithPId.getPartitionId()); } outputBuffer.writeInt(bufPosPastHeader, writtenCount); lazyAdd(bytesOutCounter, outputBuffer.position()); lazyAdd(itemsOutCounter, writtenCount); return writtenCount > 0; } catch (IOException e) { throw rethrow(e); } }
public SenderTasklet(InboundEdgeStream inboundEdgeStream, NodeEngine nodeEngine, Address destinationAddress, long executionId, int destinationVertexId, int packetSizeLimit) { this.inboundEdgeStream = inboundEdgeStream; this.packetSizeLimit = packetSizeLimit; this.connection = getMemberConnection(nodeEngine, destinationAddress); this.outputBuffer = createObjectDataOutput(nodeEngine); uncheckRun(() -> outputBuffer.write(createStreamPacketHeader( nodeEngine, executionId, destinationVertexId, inboundEdgeStream.ordinal()))); bufPosPastHeader = outputBuffer.position(); }
@Override public void writeByte(String fieldName, byte value) throws IOException { setPosition(fieldName, FieldType.BYTE); out.writeByte(value); }
@Override public void writeByteArray(String fieldName, byte[] values) throws IOException { setPosition(fieldName, FieldType.BYTE_ARRAY); out.writeByteArray(values); }
@Override public void writeBooleanArray(String fieldName, boolean[] booleans) throws IOException { setPosition(fieldName, FieldType.BOOLEAN_ARRAY); out.writeBooleanArray(booleans); }
@Override public void writeChar(String fieldName, int value) throws IOException { setPosition(fieldName, FieldType.CHAR); out.writeChar(value); }
private void cleanup() { running = false; try { sendOutput.close(); datagramPacketReceive.setData(new byte[0]); datagramPacketSend.setData(new byte[0]); } catch (Throwable ignored) { ignore(ignored); } stopLatch.countDown(); }
@Override public void writeBoolean(String fieldName, boolean value) throws IOException { setPosition(fieldName, FieldType.BOOLEAN); out.writeBoolean(value); }
@Nonnull @Override public ProgressState call() { progTracker.reset(); tryFillInbox(); if (progTracker.isDone()) { return progTracker.toProgressState(); } if (tryFillOutputBuffer()) { progTracker.madeProgress(); connection.write(new Packet(outputBuffer.toByteArray()).setPacketType(Packet.Type.JET)); } return progTracker.toProgressState(); }
private FieldDefinition setPosition(String fieldName, FieldType fieldType) throws IOException { if (raw) { throw new HazelcastSerializationException("Cannot write Portable fields after getRawDataOutput() is called!"); } FieldDefinition fd = cd.getField(fieldName); if (fd == null) { throw new HazelcastSerializationException("Invalid field name: '" + fieldName + "' for ClassDefinition {id: " + cd.getClassId() + ", version: " + cd.getVersion() + "}"); } if (writtenFields.add(fieldName)) { int pos = out.position(); int index = fd.getIndex(); out.writeInt(offset + index * INT_SIZE_IN_BYTES, pos); out.writeShort(fieldName.length()); out.writeBytes(fieldName); out.writeByte(fieldType.getId()); } else { throw new HazelcastSerializationException("Field '" + fieldName + "' has already been written!"); } return fd; }
public void send(JoinMessage joinMessage) { if (!running) { return; } final BufferObjectDataOutput out = sendOutput; synchronized (sendLock) { try { out.writeByte(Packet.VERSION); out.writeObject(joinMessage); byte[] processed = outputProcessor != null ? outputProcessor.process(out.toByteArray()) : out.toByteArray(); datagramPacketSend.setData(processed); multicastSocket.send(datagramPacketSend); out.clear(); } catch (IOException e) { // usually catching EPERM errno // see https://github.com/hazelcast/hazelcast/issues/7198 // For details about the causes look at the following discussion: // https://groups.google.com/forum/#!msg/comp.protocols.tcp-ip/Qou9Sfgr77E/mVQAPaeI-VUJ logger.warning("Sending multicast datagram failed. Exception message saying the operation is not permitted " + "usually means the underlying OS is not able to send packets at a given pace. " + "It can be caused by starting several hazelcast members in parallel when the members send " + "their join message nearly at the same time.", e); } } } }
private byte[] toBytes(Object obj, int leftPadding, boolean writeHash, PartitioningStrategy strategy, ByteOrder serializerTypeIdByteOrder) { checkNotNull(obj); checkNotNull(serializerTypeIdByteOrder); BufferPool pool = bufferPoolThreadLocal.get(); BufferObjectDataOutput out = pool.takeOutputBuffer(); try { out.position(leftPadding); SerializerAdapter serializer = serializerFor(obj); if (writeHash) { int partitionHash = calculatePartitionHash(obj, strategy); out.writeInt(partitionHash, BIG_ENDIAN); } out.writeInt(serializer.getTypeId(), serializerTypeIdByteOrder); serializer.write(out, obj); return out.toByteArray(); } catch (Throwable e) { throw handleSerializeException(obj, e); } finally { pool.returnOutputBuffer(out); } }