Beispiel #1
0
  @Override
  public void clear() {
    EntityManager em = emf.createEntityManager();
    EntityTransaction txn = em.getTransaction();

    try {
      // the clear operation often deadlocks - let's try several times
      for (int i = 0; ; ++i) {
        txn.begin();
        try {
          log.trace("Clearing JPA Store");
          String entityTable = em.getMetamodel().entity(configuration.entityClass()).getName();
          @SuppressWarnings("unchecked")
          List<Object> items = em.createQuery("FROM " + entityTable).getResultList();
          for (Object o : items) em.remove(o);
          if (configuration.storeMetadata()) {
            String metadataTable = em.getMetamodel().entity(MetadataEntity.class).getName();
            Query clearMetadata = em.createQuery("DELETE FROM " + metadataTable);
            clearMetadata.executeUpdate();
          }
          txn.commit();
          em.clear();
          break;
        } catch (Exception e) {
          log.trace("Failed to clear store", e);
          if (i >= 10) throw new JpaStoreException("Exception caught in clear()", e);
        } finally {
          if (txn != null && txn.isActive()) txn.rollback();
        }
      }
    } finally {
      em.close();
    }
  }
  public Xid[] recover(int flag) throws XAException {
    if (!configuration.isTransactionRecoveryEnabled()) {
      log.recoveryIgnored();
      return RecoveryManager.RecoveryIterator.NOTHING;
    }
    if (trace) log.trace("recover called: " + flag);

    if (isFlag(flag, TMSTARTRSCAN)) {
      recoveryIterator = recoveryManager.getPreparedTransactionsFromCluster();
      if (trace) log.tracef("Fetched a new recovery iterator: %s", recoveryIterator);
    }
    if (isFlag(flag, TMENDRSCAN)) {
      if (trace) log.trace("Flushing the iterator");
      return recoveryIterator.all();
    } else {
      // as per the spec: "TMNOFLAGS this flag must be used when no other flags are specified."
      if (!isFlag(flag, TMSTARTRSCAN) && !isFlag(flag, TMNOFLAGS))
        throw new IllegalArgumentException(
            "TMNOFLAGS this flag must be used when no other flags are specified."
                + " Received "
                + flag);
      return recoveryIterator.hasNext()
          ? recoveryIterator.next()
          : RecoveryManager.RecoveryIterator.NOTHING;
    }
  }
 public void commit(Xid xid, boolean isOnePhase) throws XAException {
   // always call prepare() - even if this is just a 1PC!
   if (isOnePhase) prepare(xid);
   if (trace) log.trace("committing TransactionXaAdapter: " + globalTx);
   try {
     LocalTxInvocationContext ctx = icc.createTxInvocationContext();
     ctx.setXaCache(this);
     if (configuration.isOnePhaseCommit()) {
       checkMarkedForRollback();
       if (trace) log.trace("Doing an 1PC prepare call on the interceptor chain");
       PrepareCommand command = commandsFactory.buildPrepareCommand(globalTx, modifications, true);
       try {
         invoker.invoke(ctx, command);
       } catch (Throwable e) {
         log.error("Error while processing 1PC PrepareCommand", e);
         throw new XAException(XAException.XAER_RMERR);
       }
     } else {
       CommitCommand commitCommand = commandsFactory.buildCommitCommand(globalTx);
       try {
         invoker.invoke(ctx, commitCommand);
       } catch (Throwable e) {
         log.error("Error while processing 1PC PrepareCommand", e);
         throw new XAException(XAException.XAER_RMERR);
       }
     }
   } finally {
     txTable.removeLocalTransaction(transaction);
     this.modifications = null;
   }
 }
  /**
   * Method that skips invocation if: - No store defined or, - The context contains
   * Flag.SKIP_CACHE_STORE or,
   */
  @Override
  protected boolean skip(InvocationContext ctx, FlagAffectedCommand command) {
    if (skip(ctx)) return true;

    if (command.hasFlag(Flag.SKIP_CACHE_STORE)) {
      log.trace("Skipping cache store since the call contain a skip cache store flag");
      return true;
    }
    if (loaderConfig.shared() && command.hasFlag(Flag.SKIP_SHARED_CACHE_STORE)) {
      log.trace(
          "Skipping cache store since it is shared and the call contain a skip shared cache store flag");
    }
    return false;
  }
Beispiel #5
0
 @Override
 public String[] getChildrenNames(ITransaction transaction, String uri) throws WebdavException {
   uri = normalizeURI(uri);
   log.tracef("GridStore.getChildrenNames(%s)", uri);
   File file = fs.getFile(root, uri);
   try {
     String[] childrenNames = null;
     if (file.isDirectory()) {
       File[] children = file.listFiles();
       if (children == null) throw new WebdavException("IO error while listing files for " + file);
       List<String> childList = new ArrayList<String>();
       for (int i = 0; i < children.length; i++) {
         String name = children[i].getName();
         childList.add(name);
         log.trace("Child " + i + ": " + name);
       }
       childrenNames = new String[childList.size()];
       childrenNames = childList.toArray(childrenNames);
     }
     return childrenNames;
   } catch (Exception e) {
     log.error("GridStore.getChildrenNames(" + uri + ") failed", e);
     throw new WebdavException(e);
   }
 }
  protected void updateStateOnNodesLeaving(Collection<Address> leavers) {
    Set<GlobalTransaction> toKill = new HashSet<GlobalTransaction>();
    for (GlobalTransaction gt : remoteTransactions.keySet()) {
      if (leavers.contains(gt.getAddress())) toKill.add(gt);
    }

    if (toKill.isEmpty())
      log.tracef(
          "No global transactions pertain to originator(s) %s who have left the cluster.", leavers);
    else
      log.tracef(
          "%s global transactions pertain to leavers list %s and need to be killed",
          toKill.size(), leavers);

    for (GlobalTransaction gtx : toKill) {
      log.tracef("Killing remote transaction originating on leaver %s", gtx);
      RollbackCommand rc = new RollbackCommand(cacheName, gtx);
      rc.init(invoker, icc, TransactionTable.this);
      try {
        rc.perform(null);
        log.tracef("Rollback of transaction %s complete.", gtx);
      } catch (Throwable e) {
        log.unableToRollbackGlobalTx(gtx, e);
      }
    }

    log.trace("Completed cleaning transactions originating on leavers");
  }
 private boolean isPutForExternalRead(FlagAffectedCommand command) {
   if (command.hasFlag(Flag.PUT_FOR_EXTERNAL_READ)) {
     log.trace("Put for external read called.  Suppressing clustered invalidation.");
     return true;
   }
   return false;
 }
Beispiel #8
0
 private void tryRollback() {
   try {
     if (transactionManager != null) transactionManager.rollback();
   } catch (Throwable t) {
     if (trace) log.trace("Could not rollback", t); // best effort
   }
 }
 public void addModification(WriteCommand mod) {
   if (trace) log.trace("Adding modification {0}. Mod list is {1}", mod, modifications);
   if (modifications == null) {
     modifications = new ArrayList<WriteCommand>(8);
   }
   modifications.add(mod);
 }
Beispiel #10
0
 /**
  * If this is a transactional cache and autoCommit is set to true then starts a transaction if
  * this is not a transactional call.
  */
 private InvocationContext getInvocationContextWithImplicitTransaction(
     EnumSet<Flag> explicitFlags, ClassLoader explicitClassLoader) {
   InvocationContext invocationContext;
   boolean txInjected = false;
   if (config.isTransactionalCache()) {
     Transaction transaction = getOngoingTransaction();
     if (transaction == null && config.isTransactionAutoCommit()) {
       try {
         transactionManager.begin();
         transaction = transactionManager.getTransaction();
         txInjected = true;
         if (trace) log.trace("Implicit transaction started!");
       } catch (Exception e) {
         throw new CacheException("Could not start transaction", e);
       }
     }
     invocationContext = getInvocationContext(transaction, explicitFlags, explicitClassLoader);
   } else {
     invocationContext = getInvocationContextForWrite(explicitFlags, explicitClassLoader);
   }
   if (txInjected) {
     ((TxInvocationContext) invocationContext).setImplicitTransaction(true);
     if (trace) log.tracef("Marked tx as implicit.");
   }
   return invocationContext;
 }
  @Override
  protected final Transaction suspendIfNeeded() {
    if (transactionManager == null) {
      return null;
    }

    try {
      switch (transactionManager.getStatus()) {
        case Status.STATUS_ACTIVE:
        case Status.STATUS_NO_TRANSACTION:
          return null;
        case Status.STATUS_MARKED_ROLLBACK:
        case Status.STATUS_PREPARED:
        case Status.STATUS_COMMITTED:
        case Status.STATUS_ROLLEDBACK:
        case Status.STATUS_UNKNOWN:
        case Status.STATUS_PREPARING:
        case Status.STATUS_COMMITTING:
        case Status.STATUS_ROLLING_BACK:
        default:
          // suspend in default and in unknown status to be safer
          return transactionManager.suspend();
      }
    } catch (Exception e) {
      if (log.isTraceEnabled()) {
        log.trace("An error occurred while trying to suspend a transaction.", e);
      }
      return null;
    }
  }
 private boolean isPutForExternalRead(InvocationContext ctx) {
   if (ctx.hasFlag(Flag.PUT_FOR_EXTERNAL_READ)) {
     log.trace("Put for external read called.  Suppressing clustered invalidation.");
     return true;
   }
   return false;
 }
  public NotifyingNotifiableFuture<Object> flushCache(
      Collection<Object> keys, Object retval, Address origin) {
    if (trace) log.tracef("Invalidating L1 caches for keys %s", keys);

    NotifyingNotifiableFuture<Object> future = new AggregatingNotifyingFutureImpl(retval, 2);

    Collection<Address> invalidationAddresses = buildInvalidationAddressList(keys, origin);

    int nodes = invalidationAddresses.size();

    if (nodes > 0) {
      // No need to invalidate at all if there is no one to invalidate!
      boolean multicast = isUseMulticast(nodes);

      if (trace)
        log.tracef(
            "There are %s nodes involved in invalidation. Threshold is: %s; using multicast: %s",
            nodes, threshold, multicast);

      if (multicast) {
        if (trace) log.tracef("Invalidating keys %s via multicast", keys);
        InvalidateCommand ic = commandsFactory.buildInvalidateFromL1Command(origin, false, keys);
        rpcManager.broadcastRpcCommandInFuture(ic, future);
      } else {
        InvalidateCommand ic = commandsFactory.buildInvalidateFromL1Command(origin, false, keys);

        // Ask the caches who have requested from us to remove
        if (trace) log.tracef("Keys %s needs invalidation on %s", keys, invalidationAddresses);
        rpcManager.invokeRemotelyInFuture(
            invalidationAddresses, ic, true, future, rpcTimeout, true);
        return future;
      }
    } else if (trace) log.trace("No L1 caches to invalidate");
    return future;
  }
 public static void waitForRehashToComplete(Cache... caches) {
   // give it 1 second to start rehashing
   // TODO Should look at the last committed view instead and check if it contains all the caches
   LockSupport.parkNanos(TimeUnit.SECONDS.toNanos(1));
   int gracetime = 30000; // 30 seconds?
   long giveup = System.currentTimeMillis() + gracetime;
   for (Cache c : caches) {
     CacheViewsManager cacheViewsManager =
         TestingUtil.extractGlobalComponent(c.getCacheManager(), CacheViewsManager.class);
     RpcManager rpcManager = TestingUtil.extractComponent(c, RpcManager.class);
     while (cacheViewsManager.getCommittedView(c.getName()).getMembers().size() != caches.length) {
       if (System.currentTimeMillis() > giveup) {
         String message =
             String.format(
                 "Timed out waiting for rehash to complete on node %s, expected member list is %s, current member list is %s!",
                 rpcManager.getAddress(),
                 Arrays.toString(caches),
                 cacheViewsManager.getCommittedView(c.getName()));
         log.error(message);
         throw new RuntimeException(message);
       }
       LockSupport.parkNanos(TimeUnit.MILLISECONDS.toNanos(100));
     }
     log.trace("Node " + rpcManager.getAddress() + " finished rehash task.");
   }
 }
  @Override
  public Object perform(InvocationContext ctx) throws Throwable {
    // It's not worth looking up the entry if we're never going to apply the change.
    if (valueMatcher == ValueMatcher.MATCH_NEVER) {
      successful = false;
      return null;
    }
    MVCCEntry e = (MVCCEntry) ctx.lookupEntry(key);
    if (e == null || e.isNull() || e.isRemoved()) {
      nonExistent = true;
      if (valueMatcher.matches(e, value, null, valueEquivalence)) {
        if (e != null) {
          e.setChanged(true);
          e.setRemoved(true);
        }
        return isConditional() ? true : null;
      } else {
        log.trace("Nothing to remove since the entry doesn't exist in the context or it is null");
        successful = false;
        return false;
      }
    }

    if (!valueMatcher.matches(e, value, null, valueEquivalence)) {
      successful = false;
      return false;
    }

    if (this instanceof EvictCommand) {
      e.setEvicted(true);
    }

    return performRemove(e, ctx);
  }
Beispiel #16
0
  @Override
  public boolean contains(Object key) {
    if (!isValidKeyType(key)) {
      return false;
    }

    EntityManager em = emf.createEntityManager();
    try {
      EntityTransaction txn = em.getTransaction();
      long txnBegin = timeService.time();
      txn.begin();
      try {
        long entityFindBegin = timeService.time();
        Object entity = em.find(configuration.entityClass(), key);
        stats.addEntityFind(timeService.time() - entityFindBegin);
        if (trace) log.trace("Entity " + key + " -> " + entity);
        try {
          if (entity == null) return false;
          if (configuration.storeMetadata()) {
            byte[] keyBytes;
            try {
              keyBytes = marshaller.objectToByteBuffer(key);
            } catch (Exception e) {
              throw new JpaStoreException("Cannot marshall key", e);
            }
            long metadataFindBegin = timeService.time();
            MetadataEntity metadata = em.find(MetadataEntity.class, keyBytes);
            stats.addMetadataFind(timeService.time() - metadataFindBegin);
            if (trace) log.trace("Metadata " + key + " -> " + toString(metadata));
            return metadata == null || metadata.expiration > timeService.wallClockTime();
          } else {
            return true;
          }
        } finally {
          txn.commit();
          stats.addReadTxCommitted(timeService.time() - txnBegin);
        }
      } catch (RuntimeException e) {
        stats.addReadTxFailed(timeService.time() - txnBegin);
        throw e;
      } finally {
        if (txn != null && txn.isActive()) txn.rollback();
      }
    } finally {
      em.close();
    }
  }
  public void finishObjectOutput(ObjectOutput oo) {
    try {
      if (log.isTraceEnabled()) log.trace("Stop marshaller");

      ((org.jboss.marshalling.Marshaller) oo).finish();
    } catch (IOException ignored) {
    }
  }
  public void finishObjectInput(ObjectInput oi) {
    try {
      if (log.isTraceEnabled()) log.trace("Stop unmarshaller");

      if (oi != null) ((Unmarshaller) oi).finish();
    } catch (IOException ignored) {
    }
  }
 public void invokeRemotelyInFuture(
     Collection<Address> recipients,
     ReplicableCommand rpc,
     NotifyingNotifiableFuture<Object> future) {
   log.trace("ControlledRpcManager.invokeRemotelyInFuture1");
   aboutToInvokeRpc(rpc);
   realOne.invokeRemotelyInFuture(recipients, rpc, future);
 }
 public void broadcastRpcCommandInFuture(
     ReplicableCommand rpcCommand, NotifyingNotifiableFuture<Object> future) {
   log.trace("ControlledRpcManager.broadcastRpcCommandInFuture1");
   failIfNeeded(rpcCommand);
   waitBefore(rpcCommand);
   realOne.broadcastRpcCommandInFuture(rpcCommand, future);
   waitAfter(rpcCommand);
 }
 public void broadcastRpcCommand(ReplicableCommand rpcCommand, boolean sync, boolean totalOrder)
     throws RpcException {
   log.trace("ControlledRpcManager.broadcastRpcCommand1");
   failIfNeeded(rpcCommand);
   waitBefore(rpcCommand);
   realOne.broadcastRpcCommand(rpcCommand, sync, totalOrder);
   waitAfter(rpcCommand);
 }
 @Override
 public AtomicHashMapDelta readObject(ObjectInput input)
     throws IOException, ClassNotFoundException {
   AtomicHashMapDelta delta = new AtomicHashMapDelta();
   delta.changeLog = (List<Operation>) input.readObject();
   if (trace) log.trace("Deserialized changeLog " + delta.changeLog);
   return delta;
 }
 private void logAfter(Connection connection, boolean checkout) {
   if (log.isTraceEnabled()) {
     String operation = checkout ? "checkout" : "release";
     try {
       log.trace(
           "DataSource after "
               + operation
               + " (NumBusyConnectionsAllUsers) : "
               + pooledDataSource.getNumBusyConnectionsAllUsers()
               + ", (NumConnectionsAllUsers) : "
               + pooledDataSource.getNumConnectionsAllUsers());
     } catch (SQLException e) {
       log.warn("Unexpected", e);
     }
     log.trace("Connection " + operation + " : " + connection);
   }
 }
 @Stop(priority = 13) // Stop after global marshaller
 public void stop() {
   started = false;
   writers.clear();
   readers.clear();
   log.trace(
       "Externalizer reader and writer maps have been cleared and constant object table was stopped");
 }
Beispiel #25
0
  @Override
  public void write(MarshalledEntry entry) {
    EntityManager em = emf.createEntityManager();

    Object entity = entry.getValue();
    MetadataEntity metadata =
        configuration.storeMetadata()
            ? new MetadataEntity(
                entry.getKeyBytes(),
                entry.getMetadataBytes(),
                entry.getMetadata() == null ? Long.MAX_VALUE : entry.getMetadata().expiryTime())
            : null;
    try {
      if (!configuration.entityClass().isAssignableFrom(entity.getClass())) {
        throw new JpaStoreException(
            String.format(
                "This cache is configured with JPA CacheStore to only store values of type %s - cannot write %s = %s",
                configuration.entityClass().getName(), entity, entity.getClass().getName()));
      } else {
        EntityTransaction txn = em.getTransaction();
        Object id = emf.getPersistenceUnitUtil().getIdentifier(entity);
        if (!entry.getKey().equals(id)) {
          throw new JpaStoreException(
              "Entity id value must equal to key of cache entry: "
                  + "key = ["
                  + entry.getKey()
                  + "], id = ["
                  + id
                  + "]");
        }
        long txnBegin = timeService.time();
        try {
          if (trace) log.trace("Writing " + entity + "(" + toString(metadata) + ")");
          txn.begin();

          long entityMergeBegin = timeService.time();
          em.merge(entity);
          stats.addEntityMerge(timeService.time() - entityMergeBegin);
          if (metadata != null && metadata.hasBytes()) {
            long metadataMergeBegin = timeService.time();
            em.merge(metadata);
            stats.addMetadataMerge(timeService.time() - metadataMergeBegin);
          }

          txn.commit();
          stats.addWriteTxCommited(timeService.time() - txnBegin);
        } catch (Exception e) {
          stats.addWriteTxFailed(timeService.time() - txnBegin);
          throw new JpaStoreException("Exception caught in write()", e);
        } finally {
          if (txn != null && txn.isActive()) txn.rollback();
        }
      }
    } finally {
      em.close();
    }
  }
Beispiel #26
0
  @Override
  public MarshalledEntry load(Object key) {
    if (!isValidKeyType(key)) {
      return null;
    }

    EntityManager em = emf.createEntityManager();
    try {
      EntityTransaction txn = em.getTransaction();
      long txnBegin = timeService.time();
      txn.begin();
      try {
        long entityFindBegin = timeService.time();
        Object entity = em.find(configuration.entityClass(), key);
        stats.addEntityFind(timeService.time() - entityFindBegin);
        try {
          if (entity == null) return null;
          InternalMetadata m = null;
          if (configuration.storeMetadata()) {
            byte[] keyBytes;
            try {
              keyBytes = marshaller.objectToByteBuffer(key);
            } catch (Exception e) {
              throw new JpaStoreException("Failed to marshall key", e);
            }
            long metadataFindBegin = timeService.time();
            MetadataEntity metadata = em.find(MetadataEntity.class, keyBytes);
            stats.addMetadataFind(timeService.time() - metadataFindBegin);
            if (metadata != null && metadata.getMetadata() != null) {
              try {
                m = (InternalMetadata) marshaller.objectFromByteBuffer(metadata.getMetadata());
              } catch (Exception e) {
                throw new JpaStoreException("Failed to unmarshall metadata", e);
              }
              if (m.isExpired(timeService.wallClockTime())) {
                return null;
              }
            }
          }
          if (trace) log.trace("Loaded " + entity + " (" + m + ")");
          return marshallerEntryFactory.newMarshalledEntry(key, entity, m);
        } finally {
          try {
            txn.commit();
            stats.addReadTxCommitted(timeService.time() - txnBegin);
          } catch (Exception e) {
            stats.addReadTxFailed(timeService.time() - txnBegin);
            throw new JpaStoreException("Failed to load entry", e);
          }
        }
      } finally {
        if (txn != null && txn.isActive()) txn.rollback();
      }
    } finally {
      em.close();
    }
  }
Beispiel #27
0
 private String toString(MetadataEntity metadata) {
   if (metadata == null || !metadata.hasBytes()) return "<no metadata>";
   try {
     return marshaller.objectFromByteBuffer(metadata.getMetadata()).toString();
   } catch (Exception e) {
     log.trace("Failed to unmarshall metadata", e);
     return "<metadata: " + e + ">";
   }
 }
 public Map<Address, Response> invokeRemotely(
     Collection<Address> recipients,
     ReplicableCommand rpcCommand,
     ResponseMode mode,
     long timeout) {
   log.trace("invokeRemotely3");
   aboutToInvokeRpc(rpcCommand);
   return realOne.invokeRemotely(recipients, rpcCommand, mode, timeout);
 }
 /**
  * Method that skips invocation if: - The store is a shared one and node storing the key is not
  * the 1st owner of the key or, - This is an L1 put operation.
  */
 @Override
 protected boolean skipKey(Object key) {
   if (loaderConfig.shared()) {
     if (!dm.getPrimaryLocation(key).equals(address)) {
       log.trace(
           "Skipping cache store since the cache loader is shared "
               + "and the caller is not the first owner of the key");
       return true;
     }
   } else {
     List<Address> addresses = dm.locate(key);
     if (isL1Put(addresses)) {
       log.trace("Skipping cache store since this is an L1 put");
       return true;
     }
   }
   return false;
 }
 @Override
 public String readString() {
   byte[] strContent = readArray();
   String readString = new String(strContent, HotRodConstants.HOTROD_STRING_CHARSET);
   if (log.isTraceEnabled()) {
     log.trace("Read string is: " + readString);
   }
   return readString;
 }