All Downloads are FREE. Search and download functionalities are using the official Maven repository.

com.gemstone.gemfire.internal.cache.UpdateEntryVersionOperation Maven / Gradle / Ivy

The newest version!
/*
 * Copyright (c) 2010-2015 Pivotal Software, Inc. All rights reserved.
 *
 * Licensed under the Apache License, Version 2.0 (the "License"); you
 * may not use this file except in compliance with the License. You
 * may obtain a copy of the License at
 *
 * http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
 * implied. See the License for the specific language governing
 * permissions and limitations under the License. See accompanying
 * LICENSE file.
 */

package com.gemstone.gemfire.internal.cache;

import java.io.DataInput;
import java.io.DataOutput;
import java.io.IOException;
import java.util.Collections;
import java.util.List;
import java.util.UUID;

import com.gemstone.gemfire.DataSerializer;
import com.gemstone.gemfire.cache.CacheEvent;
import com.gemstone.gemfire.cache.CacheWriterException;
import com.gemstone.gemfire.cache.EntryNotFoundException;
import com.gemstone.gemfire.cache.TimeoutException;
import com.gemstone.gemfire.distributed.internal.DirectReplyProcessor;
import com.gemstone.gemfire.distributed.internal.DistributionManager;
import com.gemstone.gemfire.i18n.LogWriterI18n;
import com.gemstone.gemfire.internal.InternalDataSerializer;
import com.gemstone.gemfire.internal.cache.versions.ConcurrentCacheModificationException;
import com.gemstone.gemfire.internal.i18n.LocalizedStrings;

/**
 * This operation updates Version stamp of an entry if entry is available and
 * entry version stamp has same DSID as in event's version tag.
 * 
 * @author Shobhit Agarwal
 * 
 */
public class UpdateEntryVersionOperation extends DistributedCacheOperation {

  /**
   * @param event
   */
  public UpdateEntryVersionOperation(CacheEvent event) {
    super(event);
  }

  /* (non-Javadoc)
   * @see com.gemstone.gemfire.internal.cache.DistributedCacheOperation#createMessage()
   */
  @Override
  protected CacheOperationMessage createMessage() {
    return new UpdateEntryVersionMessage(event);
  }

  @Override
  protected void initMessage(CacheOperationMessage msg, DirectReplyProcessor p) {
    super.initMessage(msg, p);
    UpdateEntryVersionMessage imsg = (UpdateEntryVersionMessage)msg;
    EntryEventImpl eei = getEvent();
    imsg.key = eei.getKey();
    imsg.eventId = eei.getEventId();
    imsg.versionTag = eei.getVersionTag();
  }

  public static class UpdateEntryVersionMessage extends CacheOperationMessage {

    protected Object key;
    protected EventID eventId = null;
    protected EntryEventImpl event = null;    
    private long tailKey = 0L; // Used for Parallel Gateway Senders
    private UUID batchUUID = null; // Used for Parallel Gateway Senders

    public UpdateEntryVersionMessage() {
    }

    public UpdateEntryVersionMessage(InternalCacheEvent ev) {
      this.event = (EntryEventImpl) ev;
    }

    @Override
    public int getDSFID() {
      return UPDATE_ENTRY_VERSION_MESSAGE;
    }

    @Override
    protected InternalCacheEvent createEvent(DistributedRegion rgn)
        throws EntryNotFoundException {
      
      if (rgn.keyRequiresRegionContext()) {
        ((KeyWithRegionContext)this.key).setRegionContext(rgn);
      }
      
      EntryEventImpl ev = EntryEventImpl.create(rgn, getOperation(), this.key,
         null /* newValue */, this.callbackArg /*callbackArg*/, true /* originRemote*/ , getSender(), false /*generateCallbacks*/);
      ev.setEventId(this.eventId);
      ev.setVersionTag(this.versionTag);
      ev.setTailKey(this.tailKey);
      ev.setBatchUUID(this.batchUUID);
      
      return ev;
    }

    @Override
    public List getOperations() {
      return Collections.singletonList(new QueuedOperation(getOperation(),
          this.key, null, null, DistributedCacheOperation
              .DESERIALIZATION_POLICY_NONE, this.callbackArg));
    }

    @Override
    protected void appendFields(StringBuilder buff) {
      super.appendFields(buff);
      buff.append("; key=");
      buff.append(this.key);
      if (this.eventId != null) {
        buff.append("; eventId=").append(this.eventId);
      }
    }

    @Override
    protected boolean operateOnRegion(CacheEvent event, DistributionManager dm)
        throws EntryNotFoundException {
      EntryEventImpl ev = (EntryEventImpl)event;
      LogWriterI18n log = ev.region.getCache().getLoggerI18n();
      DistributedRegion rgn = (DistributedRegion)ev.region;

      try {
        if (!rgn.isCacheContentProxy()) {
          if (log.finerEnabled()) {
            log.finer("UpdateEntryVersionMessage.operationOnRegion; key=" + ev.getKey());
          }

          if (rgn.getConcurrencyChecksEnabled()) {
            rgn.basicUpdateEntryVersion(ev);
          }
        }

        this.appliedOperation = true;
        return true;
      } catch (ConcurrentCacheModificationException e) {
        if (log.finerEnabled()) {
          log.finer("UpdateEntryVersionMessage.operationOnRegion; ConcurrentCacheModificationException occured for key=" + ev.getKey());
        }
        return true; // concurrent modification problems are not reported to senders
      } catch (CacheWriterException e) {
        throw new Error(LocalizedStrings.UpdateVersionOperation_CACHEWRITER_SHOULD_NOT_BE_CALLED.toLocalizedString(), e);
      } catch (TimeoutException e) {
        throw new Error(LocalizedStrings.UpdateVersionOperation_DISTRIBUTEDLOCK_SHOULD_NOT_BE_ACQUIRED.toLocalizedString(), e);
      }
    }

    @Override
    public void fromData(DataInput in)
        throws IOException, ClassNotFoundException {
      super.fromData(in);
      this.eventId = (EventID)DataSerializer.readObject(in);
      this.key = DataSerializer.readObject(in);
      this.tailKey = InternalDataSerializer.readSignedVL(in);
      this.batchUUID = InternalDataSerializer.readUUID(in);
    }

    @Override
    public void toData(DataOutput out)
        throws IOException {
      super.toData(out);
      DataSerializer.writeObject(this.eventId, out);
      DataSerializer.writeObject(this.key, out);
      
      DistributedRegion region = (DistributedRegion)this.event.getRegion();
      if (region instanceof BucketRegion) {
        PartitionedRegion pr = region.getPartitionedRegion();
        if (pr.isLocalParallelWanEnabled()) {
          InternalDataSerializer.writeSignedVL(this.event.getTailKey(), out);
        }
        else {
          InternalDataSerializer.writeSignedVL(0, out);
        }
      }
      else if(((LocalRegion)region).isUsedForSerialGatewaySenderQueue()){
        InternalDataSerializer.writeSignedVL(this.event.getTailKey(), out);
      }
      else{
        InternalDataSerializer.writeSignedVL(0, out);
      }
      InternalDataSerializer.writeUUID(this.event.getBatchUUID(),out);
    }
  }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy