private Configuration getClusteredCfg(Configuration.CacheMode mode, boolean l1) {
   Configuration cfg = getBaseCfg();
   cfg.setLockAcquisitionTimeout(60000);
   cfg.setSyncReplTimeout(60000);
   cfg.setCacheMode(mode);
   cfg.setFetchInMemoryState(false);
   if (mode.isDistributed()) {
     cfg.setL1CacheEnabled(l1);
     cfg.setL1Lifespan(120000);
   }
   return cfg;
 }
Exemple #2
0
  public static EmbeddedCacheManager createCacheContainer(
      boolean local, String passivationDir, boolean totalReplication, boolean purgeCacheLoader)
      throws Exception {
    Configuration.CacheMode mode =
        local ? CacheMode.LOCAL : (totalReplication ? CacheMode.REPL_SYNC : CacheMode.DIST_SYNC);
    GlobalConfiguration global = defaults.getGlobalConfiguration().clone();
    FluentGlobalConfiguration.TransportConfig transport = global.fluent().transport();
    if (mode.isClustered()) {
      transport
          .transportClass(JGroupsTransport.class)
          .addProperty("configurationFile", "jgroups-udp.xml");
    } else {
      transport.transportClass(null);
    }
    transport
        .clusterName("test-session")
        .strictPeerToPeer(false)
        .globalJmxStatistics()
        .jmxDomain("org.infinispan")
        .cacheManagerName("container" + containerIndex++)
        .allowDuplicateDomains(true);

    Configuration config = defaults.getDefaultConfiguration(mode).clone();
    FluentConfiguration fluent = config.fluent();
    fluent
        .syncCommitPhase(true)
        .syncRollbackPhase(true)
        .invocationBatching()
        .locking()
        .isolationLevel(IsolationLevel.REPEATABLE_READ)
        .transaction()
        .useSynchronization(false);

    if (passivationDir != null) {
      fluent
          .loaders()
          .passivation(true)
          .preload(!purgeCacheLoader)
          .addCacheLoader(
              new FileCacheStoreConfig()
                  .location(passivationDir)
                  .fetchPersistentState(mode.isReplicated())
                  .purgeOnStartup(purgeCacheLoader));
    }

    final EmbeddedCacheManager sessionContainer = new DefaultCacheManager(global, config, false);

    if (!mode.isDistributed()) {
      return sessionContainer;
    }
    // Otherwise, we need a separate cache manager for address -> jvm route mapping
    global = global.clone();
    global
        .fluent()
        .transport()
        .clusterName("test-jvmroute")
        .globalJmxStatistics()
        .cacheManagerName("container" + containerIndex++);

    config = defaults.getDefaultConfiguration(Configuration.CacheMode.REPL_SYNC).clone();
    config
        .fluent()
        .syncCommitPhase(true)
        .syncRollbackPhase(true)
        .invocationBatching()
        .locking()
        .isolationLevel(IsolationLevel.REPEATABLE_READ)
        .transaction()
        .useSynchronization(false);

    final EmbeddedCacheManager jvmRouteContainer = new DefaultCacheManager(global, config, false);

    return new EmbeddedCacheManager() {
      @Override
      public <K, V> Cache<K, V> getCache() {
        return sessionContainer.getCache();
      }

      @Override
      public <K, V> Cache<K, V> getCache(String cacheName) {
        return cacheName.equals(JVM_ROUTE_CACHE_NAME)
            ? jvmRouteContainer.<K, V>getCache()
            : sessionContainer.<K, V>getCache(cacheName);
      }

      @Override
      public void start() {
        jvmRouteContainer.start();
        try {
          sessionContainer.start();
        } catch (RuntimeException e) {
          jvmRouteContainer.stop();
          throw e;
        }
      }

      @Override
      public void stop() {
        try {
          sessionContainer.stop();
        } catch (Exception e) {
          log.warn(e.getMessage(), e);
        }
        jvmRouteContainer.stop();
      }

      @Override
      public void addListener(Object listener) {
        sessionContainer.addListener(listener);
      }

      @Override
      public void removeListener(Object listener) {
        sessionContainer.removeListener(listener);
      }

      @Override
      public Set<Object> getListeners() {
        return sessionContainer.getListeners();
      }

      @Override
      public Configuration defineConfiguration(
          String cacheName, Configuration configurationOverride) {
        return sessionContainer.defineConfiguration(cacheName, configurationOverride);
      }

      @Override
      public Configuration defineConfiguration(
          String cacheName, String templateCacheName, Configuration configurationOverride) {
        return sessionContainer.defineConfiguration(
            cacheName, templateCacheName, configurationOverride);
      }

      @Override
      public String getClusterName() {
        return sessionContainer.getClusterName();
      }

      @Override
      public List<Address> getMembers() {
        return sessionContainer.getMembers();
      }

      @Override
      public Address getAddress() {
        return sessionContainer.getAddress();
      }

      @Override
      public boolean isCoordinator() {
        return sessionContainer.isCoordinator();
      }

      @Override
      public ComponentStatus getStatus() {
        return sessionContainer.getStatus();
      }

      @Override
      public GlobalConfiguration getGlobalConfiguration() {
        return sessionContainer.getGlobalConfiguration();
      }

      @Override
      public Configuration getDefaultConfiguration() {
        return sessionContainer.getDefaultConfiguration();
      }

      @Override
      public Set<String> getCacheNames() {
        return sessionContainer.getCacheNames();
      }

      @Override
      public Address getCoordinator() {
        return sessionContainer.getCoordinator();
      }

      @Override
      public boolean isRunning(String cacheName) {
        return cacheName.equals(JVM_ROUTE_CACHE_NAME)
            ? jvmRouteContainer.isDefaultRunning()
            : sessionContainer.isRunning(cacheName);
      }

      @Override
      public boolean isDefaultRunning() {
        return sessionContainer.isDefaultRunning();
      }

      @Override
      public boolean cacheExists(String name) {
        return name.equals(JVM_ROUTE_CACHE_NAME)
            ? jvmRouteContainer.cacheExists(CacheContainer.DEFAULT_CACHE_NAME)
            : sessionContainer.cacheExists(name);
      }

      @Override
      public <K, V> Cache<K, V> getCache(String name, boolean start) {
        return name.equals(JVM_ROUTE_CACHE_NAME)
            ? jvmRouteContainer.<K, V>getCache(CacheContainer.DEFAULT_CACHE_NAME, start)
            : sessionContainer.<K, V>getCache(name, start);
      }

      @Override
      public void removeCache(String name) {
        if (name.equals(JVM_ROUTE_CACHE_NAME)) {
          jvmRouteContainer.removeCache(CacheContainer.DEFAULT_CACHE_NAME);
        } else {
          sessionContainer.removeCache(name);
        }
      }
    };
  }
  /**
   * {@inheritDoc}
   *
   * @see
   *     org.jboss.staxmapper.XMLElementWriter#writeContent(org.jboss.staxmapper.XMLExtendedStreamWriter,
   *     java.lang.Object)
   */
  @Override
  public void writeContent(XMLExtendedStreamWriter writer, SubsystemMarshallingContext context)
      throws XMLStreamException {
    context.startSubsystemElement(Namespace.CURRENT.getUri(), false);
    ModelNode model = context.getModelNode();
    if (model.isDefined()) {
      writer.writeAttribute(
          Attribute.DEFAULT_CACHE_CONTAINER.getLocalName(),
          model.require(ModelKeys.DEFAULT_CACHE_CONTAINER).asString());
      for (Property entry : model.get(ModelKeys.CACHE_CONTAINER).asPropertyList()) {

        String containerName = entry.getName();
        ModelNode container = entry.getValue();

        writer.writeStartElement(Element.CACHE_CONTAINER.getLocalName());
        writer.writeAttribute(Attribute.NAME.getLocalName(), containerName);
        this.writeRequired(writer, Attribute.DEFAULT_CACHE, container, ModelKeys.DEFAULT_CACHE);
        this.writeOptional(writer, Attribute.JNDI_NAME, container, ModelKeys.JNDI_NAME);
        this.writeOptional(
            writer, Attribute.LISTENER_EXECUTOR, container, ModelKeys.LISTENER_EXECUTOR);
        this.writeOptional(
            writer, Attribute.EVICTION_EXECUTOR, container, ModelKeys.EVICTION_EXECUTOR);
        this.writeOptional(
            writer,
            Attribute.REPLICATION_QUEUE_EXECUTOR,
            container,
            ModelKeys.REPLICATION_QUEUE_EXECUTOR);

        if (container.hasDefined(ModelKeys.ALIAS)) {
          for (ModelNode alias : container.get(ModelKeys.ALIAS).asList()) {
            writer.writeStartElement(Element.ALIAS.getLocalName());
            writer.writeCharacters(alias.asString());
            writer.writeEndElement();
          }
        }

        if (container.hasDefined(ModelKeys.SINGLETON)) {
          writer.writeStartElement(Element.TRANSPORT.getLocalName());
          ModelNode transport = container.get(ModelKeys.SINGLETON, ModelKeys.TRANSPORT);
          this.writeOptional(writer, Attribute.STACK, transport, ModelKeys.STACK);
          this.writeOptional(writer, Attribute.EXECUTOR, transport, ModelKeys.EXECUTOR);
          this.writeOptional(writer, Attribute.LOCK_TIMEOUT, transport, ModelKeys.LOCK_TIMEOUT);
          this.writeOptional(writer, Attribute.SITE, transport, ModelKeys.SITE);
          this.writeOptional(writer, Attribute.RACK, transport, ModelKeys.RACK);
          this.writeOptional(writer, Attribute.MACHINE, transport, ModelKeys.MACHINE);
          writer.writeEndElement();
        }

        // create a list of all caches in the model
        List<Property> cachesPL = new ArrayList<Property>();
        String[] cacheTypes = {
          ModelKeys.LOCAL_CACHE,
          ModelKeys.INVALIDATION_CACHE,
          ModelKeys.REPLICATED_CACHE,
          ModelKeys.DISTRIBUTED_CACHE
        };
        for (String cacheType : cacheTypes) {
          List<Property> cachesOfAType = getCachesAsPropertyList(model, containerName, cacheType);
          if (cachesOfAType != null) cachesPL.addAll(cachesOfAType);
        }

        List<ModelNode> caches = new ArrayList<ModelNode>();
        for (Property cacheEntry : cachesPL) {
          caches.add(cacheEntry.getValue());
        }

        // for (ModelNode cache: container.get(ModelKeys.CACHE).asList()) {
        for (ModelNode cache : caches) {
          Configuration.CacheMode mode =
              Configuration.CacheMode.valueOf(cache.get(ModelKeys.CACHE_MODE).asString());
          if (mode.isClustered()) {
            if (mode.isDistributed()) {
              writer.writeStartElement(Element.DISTRIBUTED_CACHE.getLocalName());
              this.writeOptional(writer, Attribute.OWNERS, cache, ModelKeys.OWNERS);
              this.writeOptional(writer, Attribute.VIRTUAL_NODES, cache, ModelKeys.VIRTUAL_NODES);
              this.writeOptional(writer, Attribute.L1_LIFESPAN, cache, ModelKeys.L1_LIFESPAN);
            } else if (mode.isInvalidation()) {
              writer.writeStartElement(Element.INVALIDATION_CACHE.getLocalName());
            } else {
              writer.writeStartElement(Element.REPLICATED_CACHE.getLocalName());
            }
            writer.writeAttribute(Attribute.MODE.getLocalName(), Mode.forCacheMode(mode).name());
            this.writeOptional(writer, Attribute.QUEUE_SIZE, cache, ModelKeys.QUEUE_SIZE);
            this.writeOptional(
                writer, Attribute.QUEUE_FLUSH_INTERVAL, cache, ModelKeys.QUEUE_FLUSH_INTERVAL);
            this.writeOptional(writer, Attribute.REMOTE_TIMEOUT, cache, ModelKeys.REMOTE_TIMEOUT);
          } else {
            writer.writeStartElement(Element.LOCAL_CACHE.getLocalName());
          }
          this.writeRequired(writer, Attribute.NAME, cache, ModelKeys.NAME);
          this.writeOptional(writer, Attribute.START, cache, ModelKeys.START);
          this.writeOptional(writer, Attribute.BATCHING, cache, ModelKeys.BATCHING);
          this.writeOptional(writer, Attribute.INDEXING, cache, ModelKeys.INDEXING);
          if (cache.hasDefined(ModelKeys.LOCKING)) {
            writer.writeStartElement(Element.LOCKING.getLocalName());
            ModelNode locking = cache.get(ModelKeys.LOCKING);
            this.writeOptional(writer, Attribute.ISOLATION, locking, ModelKeys.ISOLATION);
            this.writeOptional(writer, Attribute.STRIPING, locking, ModelKeys.STRIPING);
            this.writeOptional(
                writer, Attribute.ACQUIRE_TIMEOUT, locking, ModelKeys.ACQUIRE_TIMEOUT);
            this.writeOptional(
                writer, Attribute.CONCURRENCY_LEVEL, locking, ModelKeys.CONCURRENCY_LEVEL);
            writer.writeEndElement();
          }

          if (cache.hasDefined(ModelKeys.TRANSACTION)) {
            writer.writeStartElement(Element.TRANSACTION.getLocalName());
            ModelNode transaction = cache.get(ModelKeys.TRANSACTION);
            this.writeOptional(writer, Attribute.STOP_TIMEOUT, transaction, ModelKeys.STOP_TIMEOUT);
            this.writeOptional(writer, Attribute.MODE, transaction, ModelKeys.MODE);
            this.writeOptional(writer, Attribute.LOCKING, transaction, ModelKeys.LOCKING);
            writer.writeEndElement();
          }

          if (cache.hasDefined(ModelKeys.EVICTION)) {
            writer.writeStartElement(Element.EVICTION.getLocalName());
            ModelNode eviction = cache.get(ModelKeys.EVICTION);
            this.writeOptional(writer, Attribute.STRATEGY, eviction, ModelKeys.STRATEGY);
            this.writeOptional(writer, Attribute.MAX_ENTRIES, eviction, ModelKeys.MAX_ENTRIES);
            writer.writeEndElement();
          }

          if (cache.hasDefined(ModelKeys.EXPIRATION)) {
            writer.writeStartElement(Element.EXPIRATION.getLocalName());
            ModelNode expiration = cache.get(ModelKeys.EXPIRATION);
            this.writeOptional(writer, Attribute.MAX_IDLE, expiration, ModelKeys.MAX_IDLE);
            this.writeOptional(writer, Attribute.LIFESPAN, expiration, ModelKeys.LIFESPAN);
            this.writeOptional(writer, Attribute.INTERVAL, expiration, ModelKeys.INTERVAL);
            writer.writeEndElement();
          }

          if (cache.hasDefined(ModelKeys.STORE)) {
            ModelNode store = cache.get(ModelKeys.STORE);
            writer.writeStartElement(Element.STORE.getLocalName());
            this.writeRequired(writer, Attribute.CLASS, store, ModelKeys.CLASS);
            this.writeStoreAttributes(writer, store);
            this.writeStoreProperties(writer, store);
            writer.writeEndElement();
          }

          if (cache.hasDefined(ModelKeys.FILE_STORE)) {
            ModelNode store = cache.get(ModelKeys.FILE_STORE);
            writer.writeStartElement(Element.FILE_STORE.getLocalName());
            this.writeOptional(writer, Attribute.RELATIVE_TO, store, ModelKeys.RELATIVE_TO);
            this.writeOptional(writer, Attribute.PATH, store, ModelKeys.PATH);
            this.writeStoreAttributes(writer, store);
            this.writeStoreProperties(writer, store);
            writer.writeEndElement();
          }

          if (cache.hasDefined(ModelKeys.JDBC_STORE)) {
            ModelNode store = cache.get(ModelKeys.JDBC_STORE);
            writer.writeStartElement(Element.JDBC_STORE.getLocalName());
            this.writeRequired(writer, Attribute.DATASOURCE, store, ModelKeys.DATASOURCE);
            this.writeStoreAttributes(writer, store);
            this.writeJDBCStoreTable(writer, Element.ENTRY_TABLE, store, ModelKeys.ENTRY_TABLE);
            this.writeJDBCStoreTable(writer, Element.BUCKET_TABLE, store, ModelKeys.BUCKET_TABLE);
            this.writeStoreProperties(writer, store);
            writer.writeEndElement();
          }

          if (cache.hasDefined(ModelKeys.REMOTE_STORE)) {
            ModelNode store = cache.get(ModelKeys.REMOTE_STORE);
            writer.writeStartElement(Element.REMOTE_STORE.getLocalName());
            this.writeOptional(writer, Attribute.CACHE, store, ModelKeys.CACHE);
            this.writeOptional(writer, Attribute.SOCKET_TIMEOUT, store, ModelKeys.SOCKET_TIMEOUT);
            this.writeOptional(writer, Attribute.TCP_NO_DELAY, store, ModelKeys.TCP_NO_DELAY);
            this.writeStoreAttributes(writer, store);
            for (ModelNode remoteServer : store.get(ModelKeys.REMOTE_SERVER).asList()) {
              writer.writeStartElement(Element.REMOTE_SERVER.getLocalName());
              writer.writeAttribute(
                  Attribute.OUTBOUND_SOCKET_BINDING.getLocalName(),
                  remoteServer.get(ModelKeys.OUTBOUND_SOCKET_BINDING).asString());
              writer.writeEndElement();
            }
            this.writeStoreProperties(writer, store);
            writer.writeEndElement();
          }

          if (cache.hasDefined(ModelKeys.STATE_TRANSFER)) {
            ModelNode stateTransfer = cache.get(ModelKeys.STATE_TRANSFER);
            writer.writeStartElement(Element.STATE_TRANSFER.getLocalName());
            this.writeOptional(writer, Attribute.ENABLED, stateTransfer, ModelKeys.ENABLED);
            this.writeOptional(writer, Attribute.TIMEOUT, stateTransfer, ModelKeys.TIMEOUT);
            this.writeOptional(
                writer, Attribute.FLUSH_TIMEOUT, stateTransfer, ModelKeys.FLUSH_TIMEOUT);
            writer.writeEndElement();
          }

          if (cache.hasDefined(ModelKeys.REHASHING)) {
            ModelNode rehashing = cache.get(ModelKeys.REHASHING);
            writer.writeStartElement(Element.REHASHING.getLocalName());
            this.writeOptional(writer, Attribute.ENABLED, rehashing, ModelKeys.ENABLED);
            this.writeOptional(writer, Attribute.TIMEOUT, rehashing, ModelKeys.TIMEOUT);
            this.writeOptional(writer, Attribute.WAIT, rehashing, ModelKeys.WAIT);
            writer.writeEndElement();
          }

          writer.writeEndElement();
        }

        writer.writeEndElement();
      }
    }
    writer.writeEndElement();
  }