Search in sources :

Example 1 with CommitResponse

use of com.google.apphosting.datastore.DatastoreV3Pb.CommitResponse in project appengine-java-standard by GoogleCloudPlatform.

the class LocalDatastoreService method commitImpl.

/**
 * Requires a lock on the provided profile.
 */
private CommitResponse commitImpl(LiveTxn liveTxn, final Profile profile) {
    // assumes we already have a lock on the profile
    CommitResponse response = new CommitResponse();
    for (EntityGroupTracker tracker : liveTxn.getAllTrackers()) {
        // This will throw an exception if the entity group
        // has been modified since this transaction started.
        tracker.checkEntityGroupVersion();
    }
    int deleted = 0;
    int written = 0;
    Cost totalCost = new Cost();
    long commitTimestamp = profile.incrementAndGetCommitTimestamp();
    for (EntityGroupTracker tracker : liveTxn.getAllTrackers()) {
        Profile.EntityGroup eg = tracker.getEntityGroup();
        eg.incrementVersion();
        final Collection<EntityProto> writtenEntities = tracker.getWrittenEntities();
        final Collection<Reference> deletedKeys = tracker.getDeletedKeys();
        LocalDatastoreJob job = new WriteJob(highRepJobPolicy, eg, profile, commitTimestamp, writtenEntities, deletedKeys);
        addTo(totalCost, job.calculateJobCost());
        eg.addJob(job);
        deleted += deletedKeys.size();
        written += writtenEntities.size();
        for (EntityProto writtenEntity : writtenEntities) {
            response.addVersion().setRootEntityKey(writtenEntity.getKey()).setVersion(job.getMutationTimestamp(writtenEntity.getKey()));
        }
        for (Reference deletedKey : deletedKeys) {
            response.addVersion().setRootEntityKey(deletedKey).setVersion(job.getMutationTimestamp(deletedKey));
        }
    }
    logger.fine("committed: " + written + " puts, " + deleted + " deletes in " + liveTxn.getAllTrackers().size() + " entity groups");
    response.setCost(totalCost);
    return response;
}
Also used : Reference(com.google.storage.onestore.v3.OnestoreEntity.Reference) CommitResponse(com.google.apphosting.datastore.DatastoreV3Pb.CommitResponse) Cost(com.google.apphosting.datastore.DatastoreV3Pb.Cost) EntityProto(com.google.storage.onestore.v3.OnestoreEntity.EntityProto)

Example 2 with CommitResponse

use of com.google.apphosting.datastore.DatastoreV3Pb.CommitResponse in project appengine-java-standard by GoogleCloudPlatform.

the class LocalDatastoreService method commit.

// status
@SuppressWarnings("unused")
public CommitResponse commit(Status status, final Transaction req) {
    Profile profile = profiles.get(req.getApp());
    checkNotNull(profile);
    CommitResponse response = new CommitResponse();
    globalLock.readLock().lock();
    // Synchronized so we can't commit and rollback at the same time.
    synchronized (profile) {
        LiveTxn liveTxn;
        try {
            liveTxn = profile.removeTxn(req.getHandle());
            try {
                if (liveTxn.isDirty()) {
                    response = commitImpl(liveTxn, profile);
                } else {
                    // cost of a read-only txn is 0
                    response.setCost(new Cost().setEntityWrites(0).setIndexWrites(0));
                }
            } catch (ApplicationException e) {
                // commit failed, re-add transaction so that it can be rolled back or reset.
                profile.addTxn(req.getHandle(), new LiveTxn(clock, liveTxn.allowMultipleEg, liveTxn.originalTransactionMode, true));
                throw e;
            }
        } finally {
            globalLock.readLock().unlock();
        }
        // taskqueue tasks become durable.
        for (TaskQueueAddRequest action : liveTxn.getActions()) {
            try {
                addActionImpl(action);
            } catch (ApplicationException e) {
                logger.log(Level.WARNING, "Transactional task: " + action + " has been dropped.", e);
            }
        }
    }
    return response;
}
Also used : ApplicationException(com.google.apphosting.api.ApiProxy.ApplicationException) TaskQueueAddRequest(com.google.appengine.api.taskqueue.TaskQueuePb.TaskQueueAddRequest) CommitResponse(com.google.apphosting.datastore.DatastoreV3Pb.CommitResponse) Cost(com.google.apphosting.datastore.DatastoreV3Pb.Cost)

Aggregations

CommitResponse (com.google.apphosting.datastore.DatastoreV3Pb.CommitResponse)2 Cost (com.google.apphosting.datastore.DatastoreV3Pb.Cost)2 TaskQueueAddRequest (com.google.appengine.api.taskqueue.TaskQueuePb.TaskQueueAddRequest)1 ApplicationException (com.google.apphosting.api.ApiProxy.ApplicationException)1 EntityProto (com.google.storage.onestore.v3.OnestoreEntity.EntityProto)1 Reference (com.google.storage.onestore.v3.OnestoreEntity.Reference)1