use of com.swiftmq.impl.routing.single.smqpr.v942.CommitRequest in project swiftmq-ce by iitsoftware.
the class XARecoveryStage method doRecover.
private void doRecover(List localXids, List remoteXids) {
// Nothing to recover
if (localXids == null && remoteXids == null) {
if (ctx.traceSpace.enabled)
ctx.traceSpace.trace(ctx.routingSwiftlet.getName(), toString() + "/doRecover, nothing to do");
return;
}
List remoteRecoveryList = new ArrayList();
// Check local vs remote
if (localXids != null) {
for (int i = 0; i < localXids.size(); i++) {
XidImpl lXid = (XidImpl) localXids.get(i);
if (containsXid(remoteXids, lXid)) {
if (ctx.traceSpace.enabled)
ctx.traceSpace.trace(ctx.routingSwiftlet.getName(), toString() + "/doRecover [" + lXid + "], local prepared, remote prepared, commit 1:local, 2:remote");
// local prepared, remote prepared, commit 1:local, 2:remote
XAContext xac = ctx.xaResourceManagerSwiftlet.getXAContext(lXid);
try {
xac.commit(false);
} catch (XAContextException e) {
if (ctx.traceSpace.enabled)
ctx.traceSpace.trace(ctx.routingSwiftlet.getName(), toString() + "/doRecover, exception=" + e);
ctx.logSwiftlet.logError(ctx.routingSwiftlet.getName(), toString() + "/doRecover, commit, exception=" + e);
}
ctx.xaResourceManagerSwiftlet.removeXAContext(lXid);
remoteRecoveryList.add(new CommitRequest(lXid));
} else {
if (ctx.traceSpace.enabled)
ctx.traceSpace.trace(ctx.routingSwiftlet.getName(), toString() + "/doRecover [" + lXid + "], local prepared, remote unknown, rollback local");
// local prepared, remote unknown, rollback local
XAContext xac = ctx.xaResourceManagerSwiftlet.getXAContext(lXid);
try {
xac.rollback();
} catch (XAContextException e) {
if (ctx.traceSpace.enabled)
ctx.traceSpace.trace(ctx.routingSwiftlet.getName(), toString() + "/doRecover, exception=" + e);
ctx.logSwiftlet.logError(ctx.routingSwiftlet.getName(), toString() + "/doRecover, rollback, exception=" + e);
}
ctx.xaResourceManagerSwiftlet.removeXAContext(lXid);
}
}
}
// Check remote Xids not known locally
if (remoteXids != null) {
for (int i = 0; i < remoteXids.size(); i++) {
XidImpl rXid = (XidImpl) remoteXids.get(i);
if (!containsXid(localXids, rXid)) {
if (ctx.traceSpace.enabled)
ctx.traceSpace.trace(ctx.routingSwiftlet.getName(), toString() + "/doRecover [" + rXid + "], remote prepared, locally already committed, commit remote");
// remote prepared, locally already committed, commit remote
remoteRecoveryList.add(new CommitRequest(rXid));
}
}
}
if (remoteRecoveryList.size() > 0) {
Collections.sort(remoteRecoveryList, new Comparator() {
public int compare(Object o1, Object o2) {
int i1 = ((CommitRequest) o1).getXid().getFormatId();
int i2 = ((CommitRequest) o2).getXid().getFormatId();
return i1 == i2 ? 0 : i1 < i2 ? -1 : 1;
}
});
for (int i = 0; i < remoteRecoveryList.size(); i++) {
CommitRequest r = (CommitRequest) remoteRecoveryList.get(i);
routingConnection.getOutboundQueue().enqueue(r);
}
}
}
use of com.swiftmq.impl.routing.single.smqpr.v942.CommitRequest in project swiftmq-ce by iitsoftware.
the class XARecoveryStage method init.
protected void init() {
if (ctx.traceSpace.enabled)
ctx.traceSpace.trace(ctx.routingSwiftlet.getName(), toString() + "/init...");
visitor.setRequestHandler(com.swiftmq.impl.routing.single.smqpr.SMQRFactory.START_STAGE_REQ, new RequestHandler() {
public void visited(Request request) {
if (ctx.traceSpace.enabled)
ctx.traceSpace.trace(ctx.routingSwiftlet.getName(), XARecoveryStage.this.toString() + "/visited, request=" + request + "...");
RecoveryRequest rc = new RecoveryRequest();
rc.setBranchQualifier(recoveryBranchQ);
if (ctx.traceSpace.enabled)
ctx.traceSpace.trace(ctx.routingSwiftlet.getName(), XARecoveryStage.this.toString() + "/visited, request=" + request + ", sending request=" + rc);
routingConnection.getOutboundQueue().enqueue(rc);
routingConnection.setXaSelected(true);
}
});
visitor.setRequestHandler(com.swiftmq.impl.routing.single.smqpr.v942.SMQRFactory.RECOVERY_REPREQ, new RequestHandler() {
public void visited(Request request) {
if (ctx.traceSpace.enabled)
ctx.traceSpace.trace(ctx.routingSwiftlet.getName(), XARecoveryStage.this.toString() + "/visited, request=" + request + "...");
RecoveryReplyRequest reply = (RecoveryReplyRequest) request;
if (reply.isOk()) {
if (ctx.traceSpace.enabled)
ctx.traceSpace.trace(ctx.routingSwiftlet.getName(), XARecoveryStage.this.toString() + "/visited, request=" + request + ", do recovery");
// do recovery
List localXids = getPreparedXids(new Filter(recoveryBranchQ));
List remoteXids = reply.getXidList();
doRecover(localXids, remoteXids);
localRecovered = true;
if (remoteRecovered) {
if (ctx.traceSpace.enabled)
ctx.traceSpace.trace(ctx.routingSwiftlet.getName(), XARecoveryStage.this.toString() + "/visited, request=" + request + ", launching delivery stage");
getStageQueue().setStage(new XADeliveryStage(ctx, routingConnection));
}
if (ctx.traceSpace.enabled)
ctx.traceSpace.trace(ctx.routingSwiftlet.getName(), XARecoveryStage.this.toString() + "/visited, request=" + request + ", start remote delivery");
// start delivery
routingConnection.getOutboundQueue().enqueue(new StartDeliveryRequest());
} else {
if (ctx.traceSpace.enabled)
ctx.traceSpace.trace(ctx.routingSwiftlet.getName(), XARecoveryStage.this.toString() + "/visited, request=" + request + ", disconnect");
ctx.logSwiftlet.logError(ctx.routingSwiftlet.getName(), XARecoveryStage.this.toString() + "/exception: " + reply.getException());
ctx.networkSwiftlet.getConnectionManager().removeConnection(routingConnection.getConnection());
}
}
});
visitor.setRequestHandler(com.swiftmq.impl.routing.single.smqpr.v942.SMQRFactory.RECOVERY_REQ, new RequestHandler() {
public void visited(Request request) {
if (ctx.traceSpace.enabled)
ctx.traceSpace.trace(ctx.routingSwiftlet.getName(), XARecoveryStage.this.toString() + "/visited, request=" + request);
// It then sends a request by itself to ensure the XARecoveryStage is active at the connector side.
if (listener)
getStageQueue().enqueue(new StartStageRequest());
RecoveryRequest pr = (RecoveryRequest) request;
RecoveryReplyRequest reply = new RecoveryReplyRequest();
reply.setOk(true);
// fill xid list
reply.setXidList(getPreparedXids(new Filter(pr.getBranchQualifier())));
routingConnection.getOutboundQueue().enqueue(reply);
remoteRecovered = true;
}
});
visitor.setRequestHandler(com.swiftmq.impl.routing.single.smqpr.v942.SMQRFactory.COMMIT_REQ, new RequestHandler() {
public void visited(Request request) {
if (ctx.traceSpace.enabled)
ctx.traceSpace.trace(ctx.routingSwiftlet.getName(), XARecoveryStage.this.toString() + "/visited, request=" + request);
CommitRequest cr = (CommitRequest) request;
XAContext xac = ctx.xaResourceManagerSwiftlet.getXAContext(cr.getXid());
try {
xac.commit(false);
} catch (XAContextException e) {
if (ctx.traceSpace.enabled)
ctx.traceSpace.trace(ctx.routingSwiftlet.getName(), toString() + "/visited, request=" + request + ", exception=" + e);
ctx.logSwiftlet.logError(ctx.routingSwiftlet.getName(), toString() + "/visited, request=" + request + ", exception=" + e);
}
ctx.xaResourceManagerSwiftlet.removeXAContext(cr.getXid());
}
});
visitor.setRequestHandler(com.swiftmq.impl.routing.single.smqpr.v942.SMQRFactory.STARTDELIVERY_REQ, new RequestHandler() {
public void visited(Request request) {
if (ctx.traceSpace.enabled)
ctx.traceSpace.trace(ctx.routingSwiftlet.getName(), XARecoveryStage.this.toString() + "/visited, request=" + request + ", launching delivery stage");
if (localRecovered && remoteRecovered)
getStageQueue().setStage(new XADeliveryStage(ctx, routingConnection));
}
});
if (!listener)
getStageQueue().enqueue(new StartStageRequest());
if (ctx.traceSpace.enabled)
ctx.traceSpace.trace(ctx.routingSwiftlet.getName(), toString() + "/init done");
}
use of com.swiftmq.impl.routing.single.smqpr.v942.CommitRequest in project swiftmq-ce by iitsoftware.
the class XARecoveryStage method init.
protected void init() {
if (ctx.traceSpace.enabled)
ctx.traceSpace.trace(ctx.routingSwiftlet.getName(), toString() + "/init...");
visitor.setRequestHandler(com.swiftmq.impl.routing.single.smqpr.SMQRFactory.START_STAGE_REQ, new RequestHandler() {
public void visited(Request request) {
if (ctx.traceSpace.enabled)
ctx.traceSpace.trace(ctx.routingSwiftlet.getName(), XARecoveryStage.this.toString() + "/visited, request=" + request + "...");
RecoveryRequest rc = new RecoveryRequest();
rc.setBranchQualifier(recoveryBranchQ);
if (ctx.traceSpace.enabled)
ctx.traceSpace.trace(ctx.routingSwiftlet.getName(), XARecoveryStage.this.toString() + "/visited, request=" + request + ", sending request=" + rc);
routingConnection.getOutboundQueue().enqueue(rc);
routingConnection.setXaSelected(true);
}
});
visitor.setRequestHandler(com.swiftmq.impl.routing.single.smqpr.v400.SMQRFactory.RECOVERY_REPREQ, new RequestHandler() {
public void visited(Request request) {
if (ctx.traceSpace.enabled)
ctx.traceSpace.trace(ctx.routingSwiftlet.getName(), XARecoveryStage.this.toString() + "/visited, request=" + request + "...");
RecoveryReplyRequest reply = (RecoveryReplyRequest) request;
if (reply.isOk()) {
if (ctx.traceSpace.enabled)
ctx.traceSpace.trace(ctx.routingSwiftlet.getName(), XARecoveryStage.this.toString() + "/visited, request=" + request + ", do recovery");
// do recovery
List localXids = getPreparedXids(new Filter(recoveryBranchQ));
List remoteXids = reply.getXidList();
doRecover(localXids, remoteXids);
localRecovered = true;
if (remoteRecovered) {
if (ctx.traceSpace.enabled)
ctx.traceSpace.trace(ctx.routingSwiftlet.getName(), XARecoveryStage.this.toString() + "/visited, request=" + request + ", launching delivery stage");
getStageQueue().setStage(new XADeliveryStage(ctx, routingConnection));
}
if (ctx.traceSpace.enabled)
ctx.traceSpace.trace(ctx.routingSwiftlet.getName(), XARecoveryStage.this.toString() + "/visited, request=" + request + ", start remote delivery");
// start delivery
routingConnection.getOutboundQueue().enqueue(new StartDeliveryRequest());
} else {
if (ctx.traceSpace.enabled)
ctx.traceSpace.trace(ctx.routingSwiftlet.getName(), XARecoveryStage.this.toString() + "/visited, request=" + request + ", disconnect");
ctx.logSwiftlet.logError(ctx.routingSwiftlet.getName(), XARecoveryStage.this.toString() + "/exception: " + reply.getException());
ctx.networkSwiftlet.getConnectionManager().removeConnection(routingConnection.getConnection());
}
}
});
visitor.setRequestHandler(com.swiftmq.impl.routing.single.smqpr.v400.SMQRFactory.RECOVERY_REQ, new RequestHandler() {
public void visited(Request request) {
if (ctx.traceSpace.enabled)
ctx.traceSpace.trace(ctx.routingSwiftlet.getName(), XARecoveryStage.this.toString() + "/visited, request=" + request);
// It then sends a request by itself to ensure the XARecoveryStage is active at the connector side.
if (listener)
getStageQueue().enqueue(new StartStageRequest());
RecoveryRequest pr = (RecoveryRequest) request;
RecoveryReplyRequest reply = new RecoveryReplyRequest();
reply.setOk(true);
// fill xid list
reply.setXidList(getPreparedXids(new Filter(pr.getBranchQualifier())));
routingConnection.getOutboundQueue().enqueue(reply);
remoteRecovered = true;
}
});
visitor.setRequestHandler(com.swiftmq.impl.routing.single.smqpr.v400.SMQRFactory.COMMIT_REQ, new RequestHandler() {
public void visited(Request request) {
if (ctx.traceSpace.enabled)
ctx.traceSpace.trace(ctx.routingSwiftlet.getName(), XARecoveryStage.this.toString() + "/visited, request=" + request);
CommitRequest cr = (CommitRequest) request;
XAContext xac = ctx.xaResourceManagerSwiftlet.getXAContext(cr.getXid());
try {
xac.commit(false);
} catch (XAContextException e) {
if (ctx.traceSpace.enabled)
ctx.traceSpace.trace(ctx.routingSwiftlet.getName(), toString() + "/visited, request=" + request + ", exception=" + e);
ctx.logSwiftlet.logError(ctx.routingSwiftlet.getName(), toString() + "/visited, request=" + request + ", exception=" + e);
}
ctx.xaResourceManagerSwiftlet.removeXAContext(cr.getXid());
}
});
visitor.setRequestHandler(com.swiftmq.impl.routing.single.smqpr.v400.SMQRFactory.STARTDELIVERY_REQ, new RequestHandler() {
public void visited(Request request) {
if (ctx.traceSpace.enabled)
ctx.traceSpace.trace(ctx.routingSwiftlet.getName(), XARecoveryStage.this.toString() + "/visited, request=" + request + ", launching delivery stage");
if (localRecovered && remoteRecovered)
getStageQueue().setStage(new XADeliveryStage(ctx, routingConnection));
}
});
if (!listener)
getStageQueue().enqueue(new StartStageRequest());
if (ctx.traceSpace.enabled)
ctx.traceSpace.trace(ctx.routingSwiftlet.getName(), toString() + "/init done");
}
use of com.swiftmq.impl.routing.single.smqpr.v942.CommitRequest in project beam by apache.
the class DatastoreV1Test method datastoreWriterFnTest.
// A helper method to test DatastoreWriterFn for various batch sizes.
private void datastoreWriterFnTest(int numMutations) throws Exception {
// Create the requested number of mutations.
List<Mutation> mutations = new ArrayList<>(numMutations);
for (int i = 0; i < numMutations; ++i) {
mutations.add(makeUpsert(Entity.newBuilder().setKey(makeKey("key" + i, i + 1)).build()).build());
}
DatastoreWriterFn datastoreWriter = new DatastoreWriterFn(StaticValueProvider.of(PROJECT_ID), null, mockDatastoreFactory, new FakeWriteBatcher());
DoFnTester<Mutation, Void> doFnTester = DoFnTester.of(datastoreWriter);
doFnTester.setCloningBehavior(CloningBehavior.DO_NOT_CLONE);
doFnTester.processBundle(mutations);
int start = 0;
while (start < numMutations) {
int end = Math.min(numMutations, start + DatastoreV1.DATASTORE_BATCH_UPDATE_ENTITIES_START);
CommitRequest.Builder commitRequest = CommitRequest.newBuilder();
commitRequest.setMode(CommitRequest.Mode.NON_TRANSACTIONAL);
commitRequest.addAllMutations(mutations.subList(start, end));
// Verify all the batch requests were made with the expected mutations.
verify(mockDatastore, times(1)).commit(commitRequest.build());
start = end;
}
}
use of com.swiftmq.impl.routing.single.smqpr.v942.CommitRequest in project pgadapter by GoogleCloudPlatform.
the class JdbcMockServerTest method testCopyIn.
@Test
public void testCopyIn() throws SQLException, IOException {
setupCopyInformationSchemaResults();
try (Connection connection = DriverManager.getConnection(createUrl())) {
CopyManager copyManager = new CopyManager(connection.unwrap(BaseConnection.class));
copyManager.copyIn("COPY users FROM STDIN;", new StringReader("5\t5\t5\n6\t6\t6\n7\t7\t7\n"));
}
List<CommitRequest> commitRequests = mockSpanner.getRequestsOfType(CommitRequest.class);
assertEquals(1, commitRequests.size());
CommitRequest commitRequest = commitRequests.get(0);
assertEquals(1, commitRequest.getMutationsCount());
Mutation mutation = commitRequest.getMutations(0);
assertEquals(OperationCase.INSERT, mutation.getOperationCase());
assertEquals(3, mutation.getInsert().getValuesCount());
}
Aggregations