use of org.neo4j.kernel.impl.store.StoreId in project neo4j by neo4j.
the class Protocol method deserializeResponse.
public <PAYLOAD> Response<PAYLOAD> deserializeResponse(BlockingReadHandler<ChannelBuffer> reader, ByteBuffer input, long timeout, Deserializer<PAYLOAD> payloadDeserializer, ResourceReleaser channelReleaser, final LogEntryReader<ReadableClosablePositionAwareChannel> entryReader) throws IOException {
final DechunkingChannelBuffer dechunkingBuffer = new DechunkingChannelBuffer(reader, timeout, internalProtocolVersion, applicationProtocolVersion);
PAYLOAD response = payloadDeserializer.read(dechunkingBuffer, input);
StoreId storeId = readStoreId(dechunkingBuffer, input);
// Response type is what previously was a byte saying how many data sources there were in the
// coming transaction stream response. For backwards compatibility we keep it as a byte and we introduce
// the transaction obligation response type as -1
byte responseType = dechunkingBuffer.readByte();
if (responseType == TransactionObligationResponse.RESPONSE_TYPE) {
// It is a transaction obligation response
long obligationTxId = dechunkingBuffer.readLong();
return new TransactionObligationResponse<>(response, storeId, obligationTxId, channelReleaser);
}
// It's a transaction stream in this response
TransactionStream transactions = visitor -> {
NetworkReadableClosableChannel channel = new NetworkReadableClosableChannel(dechunkingBuffer);
try (PhysicalTransactionCursor<ReadableClosablePositionAwareChannel> cursor = new PhysicalTransactionCursor<>(channel, entryReader)) {
while (cursor.next() && !visitor.visit(cursor.get())) {
}
}
};
return new TransactionStreamResponse<>(response, storeId, transactions, channelReleaser);
}
use of org.neo4j.kernel.impl.store.StoreId in project neo4j by neo4j.
the class SwitchToSlave method switchToSlave.
/**
* Performs a switch to the slave state. Starts the communication endpoints, switches components to the slave state
* and ensures that the current database is appropriate for this cluster. It also broadcasts the appropriate
* Slave Is Available event
*
* @param haCommunicationLife The LifeSupport instance to register the network facilities required for
* communication with the rest of the cluster
* @param me The URI this instance must bind to
* @param masterUri The URI of the master for which this instance must become slave to
* @param cancellationRequest A handle for gracefully aborting the switch
* @return The URI that was broadcasted as the slave endpoint or null if the task was cancelled
*/
public URI switchToSlave(LifeSupport haCommunicationLife, URI me, URI masterUri, CancellationRequest cancellationRequest) throws Throwable {
URI slaveUri;
boolean success = false;
monitor.switchToSlaveStarted();
// Wait a short while for current transactions to stop first, just to be nice.
// We can't wait forever since switching to our designated role is quite important.
Clock clock = Clocks.systemClock();
long deadline = clock.millis() + config.get(HaSettings.internal_state_switch_timeout);
while (transactionCounters.getNumberOfActiveTransactions() > 0 && clock.millis() < deadline) {
parkNanos(MILLISECONDS.toNanos(10));
}
try {
InstanceId myId = config.get(ClusterSettings.server_id);
userLog.info("ServerId %s, moving to slave for master %s", myId, masterUri);
// since we are here it must already have been set from outside
assert masterUri != null;
idGeneratorFactory.switchToSlave();
copyStoreFromMasterIfNeeded(masterUri, me, cancellationRequest);
/*
* The following check is mandatory, since the store copy can be cancelled and if it was actually
* happening then we can't continue, as there is no store in place
*/
if (cancellationRequest.cancellationRequested()) {
msgLog.info("Switch to slave cancelled during store copy if no local store is present.");
return null;
}
/*
* We get here either with a fresh store from the master copy above so we need to
* start the ds or we already had a store, so we have already started the ds. Either way,
* make sure it's there.
*/
NeoStoreDataSource neoDataSource = neoDataSourceSupplier.get();
neoDataSource.afterModeSwitch();
StoreId myStoreId = neoDataSource.getStoreId();
boolean consistencyChecksExecutedSuccessfully = executeConsistencyChecks(transactionIdStoreSupplier.get(), masterUri, me, myStoreId, cancellationRequest);
if (!consistencyChecksExecutedSuccessfully) {
msgLog.info("Switch to slave cancelled due to consistency check failure.");
return null;
}
if (cancellationRequest.cancellationRequested()) {
msgLog.info("Switch to slave cancelled after consistency checks.");
return null;
}
// no exception were thrown and we can proceed
slaveUri = startHaCommunication(haCommunicationLife, neoDataSource, me, masterUri, myStoreId, cancellationRequest);
if (slaveUri == null) {
msgLog.info("Switch to slave unable to connect.");
return null;
}
success = true;
userLog.info("ServerId %s, successfully moved to slave for master %s", myId, masterUri);
} finally {
monitor.switchToSlaveCompleted(success);
}
return slaveUri;
}
use of org.neo4j.kernel.impl.store.StoreId in project neo4j by neo4j.
the class MasterClient214 method handshake.
@Override
public Response<HandshakeResult> handshake(final long txId, StoreId storeId) {
Serializer serializer = buffer -> buffer.writeLong(txId);
Deserializer<HandshakeResult> deserializer = (buffer, temporaryBuffer) -> new HandshakeResult(buffer.readLong(), buffer.readLong());
return sendRequest(requestTypes.type(HaRequestTypes.Type.HANDSHAKE), RequestContext.EMPTY, serializer, deserializer, storeId, ResponseUnpacker.TxHandler.NO_OP_TX_HANDLER);
}
use of org.neo4j.kernel.impl.store.StoreId in project neo4j by neo4j.
the class MasterClientTest method endLockSessionDoesNotUnpackResponse.
@Test
public void endLockSessionDoesNotUnpackResponse() throws Throwable {
StoreId storeId = new StoreId(1, 2, 3, 4, 5);
long txChecksum = 123;
long lastAppliedTx = 5;
ResponseUnpacker responseUnpacker = mock(ResponseUnpacker.class);
MasterImpl.SPI masterImplSPI = MasterImplTest.mockedSpi(storeId);
when(masterImplSPI.packTransactionObligationResponse(any(RequestContext.class), Matchers.anyObject())).thenReturn(Response.empty());
when(masterImplSPI.getTransactionChecksum(anyLong())).thenReturn(txChecksum);
newMasterServer(masterImplSPI);
MasterClient client = newMasterClient320(storeId, responseUnpacker);
HandshakeResult handshakeResult;
try (Response<HandshakeResult> handshakeResponse = client.handshake(1, storeId)) {
handshakeResult = handshakeResponse.response();
}
verify(responseUnpacker).unpackResponse(any(Response.class), any(TxHandler.class));
reset(responseUnpacker);
RequestContext context = new RequestContext(handshakeResult.epoch(), 1, 1, lastAppliedTx, txChecksum);
client.endLockSession(context, false);
verify(responseUnpacker, never()).unpackResponse(any(Response.class), any(TxHandler.class));
}
use of org.neo4j.kernel.impl.store.StoreId in project neo4j by neo4j.
the class MasterClientTest method newClientsShouldNotIgnoreStoreIdDifferences.
@Test(expected = MismatchingStoreIdException.class)
public void newClientsShouldNotIgnoreStoreIdDifferences() throws Throwable {
// Given
MasterImpl.SPI masterImplSPI = MasterImplTest.mockedSpi(StoreIdTestFactory.newStoreIdForCurrentVersion(1, 2, 3, 4));
when(masterImplSPI.getTransactionChecksum(anyLong())).thenReturn(5L);
newMasterServer(masterImplSPI);
StoreId storeId = StoreIdTestFactory.newStoreIdForCurrentVersion(5, 6, 7, 8);
MasterClient masterClient = newMasterClient320(storeId);
// When
masterClient.handshake(1, storeId);
}
Aggregations