@Test
 public void testWANClusteringActivePassive() throws Exception {
   Config c1 = new Config();
   Config c2 = new Config();
   c1.getGroupConfig().setName("newyork");
   c1.addWanReplicationConfig(
       new WanReplicationConfig()
           .setName("my-wan")
           .addTargetClusterConfig(
               new WanTargetClusterConfig().addEndpoint("127.0.0.1:5702").setGroupName("london")));
   c1.getMapConfig("default")
       .setWanReplicationRef(
           new WanReplicationRef().setName("my-wan").setMergePolicy(PassThroughMergePolicy.NAME));
   c2.getGroupConfig().setName("london");
   c2.getMapConfig("default")
       .setWanReplicationRef(
           new WanReplicationRef().setName("my-wan").setMergePolicy(PassThroughMergePolicy.NAME));
   HazelcastInstance h10 = Hazelcast.newHazelcastInstance(c1);
   HazelcastInstance h20 = Hazelcast.newHazelcastInstance(c2);
   int size = 1000;
   MergeLatch mergeLatch2 = new MergeLatch(size);
   getConcurrentMapManager(h20).addWanMergeListener(mergeLatch2);
   for (int i = 0; i < size; i++) {
     h10.getMap("default").put(i, "value" + i);
   }
   assertTrue("Latch state: " + mergeLatch2, mergeLatch2.await(60, TimeUnit.SECONDS));
   Thread.sleep(1000);
   assertEquals(size, mergeLatch2.totalOperations());
   assertEquals(size, h10.getMap("default").size());
   assertEquals(size, h20.getMap("default").size());
   for (int i = 0; i < size; i++) {
     assertEquals("value" + i, h20.getMap("default").get(i));
   }
 }
  private void testConfig2Xml2Config(String fileName) {
    String pass = "******";
    final Config config = new ClasspathXmlConfig(fileName);
    config.getGroupConfig().setPassword(pass);

    final String xml = new ConfigXmlGenerator(true).generate(config);
    final Config config2 = new InMemoryXmlConfig(xml);
    config2.getGroupConfig().setPassword(pass);

    assertTrue(config.isCompatible(config2));
    assertTrue(config2.isCompatible(config));
  }
Beispiel #3
0
 /**
  * @param config
  * @return true if config is compatible with this one, false if config belongs to another group
  * @throws RuntimeException if map, queue, topic configs are incompatible
  */
 public boolean isCompatible(final Config config) {
   if (config == null) {
     throw new IllegalArgumentException("Expected not null config");
   }
   if (!this.groupConfig.getName().equals(config.getGroupConfig().getName())) {
     return false;
   }
   if (!this.groupConfig.getPassword().equals(config.getGroupConfig().getPassword())) {
     throw new HazelcastException("Incompatible group password");
   }
   checkMapConfigCompatible(config);
   return true;
 }
 private Config newConfig() {
   Config config = new Config();
   config.setProperty(GroupProperty.MERGE_FIRST_RUN_DELAY_SECONDS, "5");
   config.setProperty(GroupProperty.MERGE_NEXT_RUN_DELAY_SECONDS, "3");
   config.getGroupConfig().setName(generateRandomString(10));
   return config;
 }
 @Test
 public void testGroupConfig() {
   GroupConfig groupConfig = config.getGroupConfig();
   assertNotNull(groupConfig);
   assertEquals("spring-group", groupConfig.getName());
   assertEquals("spring-group-pass", groupConfig.getPassword());
 }
  protected HazelcastInstance hazelcastInstance() {
    Config config = new Config();

    config.addCacheConfig(
        new CacheSimpleConfig()
            .setName("result-cache")
            .setInMemoryFormat(InMemoryFormat.BINARY)
            .setEvictionConfig(new EvictionConfig().setEvictionPolicy(EvictionPolicy.LRU))
            .setExpiryPolicyFactoryConfig(
                new CacheSimpleConfig.ExpiryPolicyFactoryConfig(
                    new CacheSimpleConfig.ExpiryPolicyFactoryConfig.TimedExpiryPolicyFactoryConfig(
                        CacheSimpleConfig.ExpiryPolicyFactoryConfig.TimedExpiryPolicyFactoryConfig
                            .ExpiryPolicyType.ACCESSED,
                        new CacheSimpleConfig.ExpiryPolicyFactoryConfig.DurationConfig(
                            20, TimeUnit.MINUTES)))));

    NetworkConfig networkConfig = new NetworkConfig();
    networkConfig.setPort(5712).setPortAutoIncrement(true);

    config.setNetworkConfig(networkConfig);
    config.setGroupConfig(new GroupConfig("ach-validation", "ach-validation"));

    TcpClusterConfigurator.configureNetwork(config.getNetworkConfig());
    TcpClusterConfigurator.configureGroup(config.getGroupConfig());

    return Hazelcast.newHazelcastInstance(config);
  }
 @Override
 public String name() {
   try {
     if (cluster.getInstance() != null) {
       HazelcastInstance instance = cluster.getInstance();
       if (instance.getConfig() != null) {
         Config config = instance.getConfig();
         if (config.getGroupConfig() != null) {
           return "hazelcast/" + config.getGroupConfig().getName();
         }
       }
     }
   } catch (Exception ignore) {
   }
   return "hazelcast";
 }
  @Test
  public void loadingThroughSystemProperty_existingClasspathResource() throws IOException {
    System.setProperty("hazelcast.config", "classpath:test-hazelcast.xml");

    XmlConfigBuilder configBuilder = new XmlConfigBuilder();
    Config config = configBuilder.build();
    assertEquals("foobar", config.getGroupConfig().getName());
  }
 @Start
 public void startHazel() {
   Config configApp = new Config();
   configApp.getGroupConfig().setName("KGroup_HazelGroup_" + getName());
   configApp.setInstanceName("KGroup_HazelGroup_" + getNodeName());
   hazelInstance = Hazelcast.newHazelcastInstance(configApp);
   Cluster cluster = hazelInstance.getCluster();
   cluster.addMembershipListener(this);
 }
  @Before
  public void init() {
    Config config = new Config();
    config.getNetworkConfig().getJoin().getMulticastConfig().setEnabled(false);
    config.getNetworkConfig().setPort(5701);
    config.getGroupConfig().setName("cluster1");
    config.getGroupConfig().setPassword("cluster1pass");

    final HazelcastInstance hz1 = Hazelcast.newHazelcastInstance(config);

    Config config2 = new Config();
    config2.getNetworkConfig().getJoin().getMulticastConfig().setEnabled(false);
    config2.getNetworkConfig().setPort(5702);
    config2.getGroupConfig().setName("cluster2");
    config2.getGroupConfig().setPassword("cluster2pass");

    final HazelcastInstance hz2 = Hazelcast.newHazelcastInstance(config2);
  }
Beispiel #11
0
  public Node(HazelcastInstanceImpl hazelcastInstance, Config config, NodeContext nodeContext) {
    this.hazelcastInstance = hazelcastInstance;
    this.config = config;
    this.configClassLoader = config.getClassLoader();
    this.groupProperties = new GroupProperties(config);
    this.buildInfo = BuildInfoProvider.getBuildInfo();

    String loggingType = groupProperties.getString(GroupProperty.LOGGING_TYPE);
    loggingService =
        new LoggingServiceImpl(config.getGroupConfig().getName(), loggingType, buildInfo);
    final AddressPicker addressPicker = nodeContext.createAddressPicker(this);
    try {
      addressPicker.pickAddress();
    } catch (Throwable e) {
      throw ExceptionUtil.rethrow(e);
    }

    final ServerSocketChannel serverSocketChannel = addressPicker.getServerSocketChannel();
    try {
      address = addressPicker.getPublicAddress();
      final Map<String, Object> memberAttributes =
          findMemberAttributes(config.getMemberAttributeConfig().asReadOnly());
      localMember =
          new MemberImpl(
              address, true, createMemberUuid(address), hazelcastInstance, memberAttributes);
      loggingService.setThisMember(localMember);
      logger = loggingService.getLogger(Node.class.getName());
      hazelcastThreadGroup =
          new HazelcastThreadGroup(hazelcastInstance.getName(), logger, configClassLoader);
      nodeExtension = NodeExtensionFactory.create(configClassLoader);
      nodeExtension.beforeStart(this);

      serializationService = nodeExtension.createSerializationService();
      securityContext =
          config.getSecurityConfig().isEnabled() ? nodeExtension.getSecurityContext() : null;

      nodeEngine = new NodeEngineImpl(this);

      clientEngine = new ClientEngineImpl(this);
      connectionManager = nodeContext.createConnectionManager(this, serverSocketChannel);
      partitionService = new InternalPartitionServiceImpl(this);
      clusterService = new ClusterServiceImpl(this);
      textCommandService = new TextCommandServiceImpl(this);
      nodeExtension.printNodeInfo(this);
      multicastService =
          createMulticastService(addressPicker.getBindAddress(), this, config, logger);
      discoveryService = createDiscoveryService(config);
      initializeListeners(config);
      joiner = nodeContext.createJoiner(this);
    } catch (Throwable e) {
      try {
        serverSocketChannel.close();
      } catch (Throwable ignored) {
      }
      throw ExceptionUtil.rethrow(e);
    }
  }
Beispiel #12
0
 private static Config buildConfig(boolean multicastEnabled) {
   Config c = new Config();
   c.getGroupConfig().setName("group").setPassword("pass");
   c.setProperty(GroupProperties.PROP_MERGE_FIRST_RUN_DELAY_SECONDS, "10");
   c.setProperty(GroupProperties.PROP_MERGE_NEXT_RUN_DELAY_SECONDS, "5");
   c.setProperty(GroupProperties.PROP_MAX_NO_HEARTBEAT_SECONDS, "10");
   c.setProperty(GroupProperties.PROP_MASTER_CONFIRMATION_INTERVAL_SECONDS, "2");
   c.setProperty(GroupProperties.PROP_MAX_NO_MASTER_CONFIRMATION_SECONDS, "10");
   c.setProperty(GroupProperties.PROP_MEMBER_LIST_PUBLISH_INTERVAL_SECONDS, "10");
   final NetworkConfig networkConfig = c.getNetworkConfig();
   networkConfig.getJoin().getMulticastConfig().setEnabled(multicastEnabled);
   networkConfig.getJoin().getTcpIpConfig().setEnabled(!multicastEnabled);
   networkConfig.setPortAutoIncrement(false);
   return c;
 }
Beispiel #13
0
 /*
    github issue 585
 */
 @Test
 public void testIssue585ZeroTTLShouldPreventEvictionWithSet() throws InterruptedException {
   Config config = new Config();
   config.getGroupConfig().setName("testIssue585ZeroTTLShouldPreventEvictionWithSet");
   NearCacheConfig nearCacheConfig = new NearCacheConfig();
   config.getMapConfig("default").setNearCacheConfig(nearCacheConfig);
   int n = 1;
   TestHazelcastInstanceFactory factory = createHazelcastInstanceFactory(n);
   HazelcastInstance h = factory.newHazelcastInstance(config);
   IMap<String, String> map = h.getMap("testIssue585ZeroTTLShouldPreventEvictionWithSet");
   map.set("key", "value", 1, TimeUnit.SECONDS);
   map.set("key", "value2", 0, TimeUnit.SECONDS);
   Thread.sleep(2000);
   assertEquals("value2", map.get("key"));
   h.getLifecycleService().shutdown();
 }
  @Before
  public void setUp() {
    Hazelcast.shutdownAll();

    instanceFactory = createHazelcastInstanceFactory(10000);
    config = new Config();
    config.getGroupConfig().setName(generateRandomString(10));
    MapConfig mapConfig = new MapConfig("map");
    // mapConfig.setBackupCount(0);
    config.addMapConfig(mapConfig);
    hz = createHazelcastInstance();

    for (int i = 0; i < INITIAL_MEMBER_COUNT; i++) {
      queue.add(createHazelcastInstance());
    }
  }
 @Test
 public void testDisablingSystemLogs() throws Exception {
   Config config = new Config();
   config.setProperty(GroupProperties.PROP_SYSTEM_LOG_ENABLED, "true");
   config.getGroupConfig().setName("testDisablingSystemLogs");
   HazelcastInstance instance = Hazelcast.newHazelcastInstance(config);
   HazelcastInstance instance2 = Hazelcast.newHazelcastInstance(config);
   instance.getMap("map").put("key", "value");
   Node node = TestUtil.getNode(instance);
   assertTrue(node.getSystemLogService().getLogBundle().size() > 0);
   Hazelcast.shutdownAll();
   config.setProperty(GroupProperties.PROP_SYSTEM_LOG_ENABLED, "false");
   instance = Hazelcast.newHazelcastInstance(config);
   instance2 = Hazelcast.newHazelcastInstance(config);
   instance.getMap("map").put("key2", "value2");
   assertTrue(node.getSystemLogService().getLogBundle().size() == 0);
 }
 private Config newConfig(String mergePolicy, String mapName) {
   Config config = new Config();
   setCommonProperties(config);
   config.setProperty(GroupProperty.MERGE_FIRST_RUN_DELAY_SECONDS.getName(), "5");
   config.setProperty(GroupProperty.MERGE_NEXT_RUN_DELAY_SECONDS.getName(), "3");
   MapConfig mapConfig = config.getMapConfig(mapName);
   mapConfig.setMergePolicy(mergePolicy);
   mapConfig.setBackupCount(1);
   mapConfig.setReadBackupData(true);
   mapConfig.setStatisticsEnabled(true);
   mapConfig.setMaxIdleSeconds(0);
   mapConfig.setTimeToLiveSeconds(0);
   mapConfig.addMapIndexConfig(new MapIndexConfig("id", false));
   config.setNetworkConfig(this.getLocalhostTcpIpNetworkConfig(6701));
   config.getGroupConfig().setName(mapName);
   config.getGroupConfig().setPassword(mapName);
   return config;
 }
  /**
   * 通过加载HZ的配置文件,动态创建HZ集群
   *
   * @param configPath hz的配置文件
   */
  public HazelcastStoreManager(HazelcastProperties hazelcastProperties) {
    //        Properties prop = new Properties();
    //        try {
    //            prop = loadConf(null);
    //        } catch (IOException e) {
    //            LOGGER.warn("load conf error,use default config");
    //        }
    Config cfg = new Config();
    cfg.getGroupConfig().setName(hazelcastProperties.getGroupUserName());
    cfg.getGroupConfig().setPassword(hazelcastProperties.getGroupPassword());

    cfg.setInstanceName(hazelcastProperties.getInstanceName());

    // cfg.getQueueConfig(EVENT_QUEUE).addItemListenerConfig(new
    // ItemListenerConfig(this.hazelcastQueueItemListener,true));
    if (hazelcastProperties.getMancenter().isEnable()
        && StringUtils.isNotBlank(hazelcastProperties.getMancenter().getUrl())) {
      cfg.getManagementCenterConfig().setEnabled(true);
      cfg.getManagementCenterConfig().setUrl(hazelcastProperties.getMancenter().getUrl());
    }
    System.setProperty("hazelcast.socket.bind.any", "false");
    String ip = "127.0.0.1";
    try {
      ip = InetAddress.getLocalHost().getHostAddress();
    } catch (UnknownHostException e) {
      LOGGER.warn("get ip error, {}", e.getMessage());
    }
    LOGGER.info("local memchine ip: {}", ip);
    cfg.getProperties(); // .getGroupProperties().SOCKET_SERVER_BIND_ANY
    cfg.getNetworkConfig().getInterfaces().addInterface(ip);
    cfg.getNetworkConfig().getInterfaces().setEnabled(true);

    JoinConfig join = cfg.getNetworkConfig().getJoin();
    TcpIpConfig tcpIpConfig = join.getTcpIpConfig();
    tcpIpConfig.addMember(hazelcastProperties.getMembers());
    if (!tcpIpConfig.getMembers().contains("127.0.0.1")) {
      tcpIpConfig.addMember("127.0.0.1");
    }
    LOGGER.info("hazelcast members in prop:{}", tcpIpConfig.getMembers());
    tcpIpConfig.setEnabled(true);
    this.hazelcast = Hazelcast.newHazelcastInstance(cfg);
    this.cacheManager = new HazelcastCacheManager(this.hazelcast);
  }
Beispiel #18
0
  public ConfigCheck createConfigCheck() {
    final ConfigCheck configCheck = new ConfigCheck();
    final GroupConfig groupConfig = config.getGroupConfig();
    final PartitionGroupConfig partitionGroupConfig = config.getPartitionGroupConfig();
    final boolean partitionGroupEnabled =
        partitionGroupConfig != null && partitionGroupConfig.isEnabled();

    PartitionGroupConfig.MemberGroupType memberGroupType =
        partitionGroupEnabled
            ? partitionGroupConfig.getGroupType()
            : PartitionGroupConfig.MemberGroupType.PER_MEMBER;
    configCheck
        .setGroupName(groupConfig.getName())
        .setGroupPassword(groupConfig.getPassword())
        .setJoinerType(joiner != null ? joiner.getType() : "")
        .setPartitionGroupEnabled(partitionGroupEnabled)
        .setMemberGroupType(memberGroupType);
    return configCheck;
  }
Beispiel #19
0
  /*
     github issue 304
  */
  @Test
  public void testIssue304EvictionDespitePut() throws InterruptedException {
    Config c = new Config();
    c.getGroupConfig().setName("testIssue304EvictionDespitePut");
    final HashMap<String, MapConfig> mapConfigs = new HashMap<String, MapConfig>();
    final MapConfig value = new MapConfig();
    value.setMaxIdleSeconds(3);
    mapConfigs.put("default", value);
    c.setMapConfigs(mapConfigs);
    final Properties properties = new Properties();
    properties.setProperty("hazelcast.map.cleanup.delay.seconds", "1"); // we need faster cleanups
    c.setProperties(properties);
    int n = 1;
    TestHazelcastInstanceFactory factory = createHazelcastInstanceFactory(n);
    final HazelcastInstance hazelcastInstance = factory.newHazelcastInstance(c);

    IMap<String, Long> map = hazelcastInstance.getMap("testIssue304EvictionDespitePutMap");
    final AtomicInteger evictCount = new AtomicInteger(0);
    map.addEntryListener(
        new EntryListener<String, Long>() {
          public void entryAdded(EntryEvent<String, Long> event) {}

          public void entryRemoved(EntryEvent<String, Long> event) {}

          public void entryUpdated(EntryEvent<String, Long> event) {}

          public void entryEvicted(EntryEvent<String, Long> event) {
            evictCount.incrementAndGet();
          }
        },
        true);

    String key = "key";
    for (int i = 0; i < 5; i++) {
      map.put(key, System.currentTimeMillis());
      Thread.sleep(1000);
    }
    assertEquals(evictCount.get(), 0);
    assertNotNull(map.get(key));
    hazelcastInstance.getLifecycleService().shutdown();
  }
  @Test
  public void loadingThroughSystemProperty_existingFile() throws IOException {
    String xml =
        "<hazelcast xmlns=\"http://www.hazelcast.com/schema/config\">\n"
            + "    <group>\n"
            + "        <name>foobar</name>\n"
            + "        <password>dev-pass</password>\n"
            + "    </group>"
            + "</hazelcast>";

    File file = File.createTempFile("foo", "bar");
    file.deleteOnExit();
    PrintWriter writer = new PrintWriter(file, "UTF-8");
    writer.println(xml);
    writer.close();

    System.setProperty("hazelcast.config", file.getAbsolutePath());

    XmlConfigBuilder configBuilder = new XmlConfigBuilder();
    Config config = configBuilder.build();
    assertEquals("foobar", config.getGroupConfig().getName());
  }
Beispiel #21
0
 public Node(HazelcastInstanceImpl hazelcastInstance, Config config, NodeContext nodeContext) {
   this.hazelcastInstance = hazelcastInstance;
   this.threadGroup = hazelcastInstance.threadGroup;
   this.config = config;
   configClassLoader = config.getClassLoader();
   this.groupProperties = new GroupProperties(config);
   SerializationService ss;
   try {
     ss =
         new SerializationServiceBuilder()
             .setClassLoader(configClassLoader)
             .setConfig(config.getSerializationConfig())
             .setManagedContext(hazelcastInstance.managedContext)
             .setHazelcastInstance(hazelcastInstance)
             .build();
   } catch (Exception e) {
     throw ExceptionUtil.rethrow(e);
   }
   serializationService = (SerializationServiceImpl) ss;
   systemLogService = new SystemLogService(groupProperties.SYSTEM_LOG_ENABLED.getBoolean());
   final AddressPicker addressPicker = nodeContext.createAddressPicker(this);
   try {
     addressPicker.pickAddress();
   } catch (Throwable e) {
     throw ExceptionUtil.rethrow(e);
   }
   final ServerSocketChannel serverSocketChannel = addressPicker.getServerSocketChannel();
   address = addressPicker.getPublicAddress();
   localMember = new MemberImpl(address, true, UuidUtil.createMemberUuid(address));
   String loggingType = groupProperties.LOGGING_TYPE.getString();
   loggingService =
       new LoggingServiceImpl(
           systemLogService, config.getGroupConfig().getName(), loggingType, localMember);
   logger = loggingService.getLogger(Node.class.getName());
   initializer = NodeInitializerFactory.create(configClassLoader);
   try {
     initializer.beforeInitialize(this);
   } catch (Throwable e) {
     try {
       serverSocketChannel.close();
     } catch (Throwable ignored) {
     }
     throw ExceptionUtil.rethrow(e);
   }
   securityContext =
       config.getSecurityConfig().isEnabled() ? initializer.getSecurityContext() : null;
   nodeEngine = new NodeEngineImpl(this);
   clientEngine = new ClientEngineImpl(this);
   connectionManager = nodeContext.createConnectionManager(this, serverSocketChannel);
   partitionService = new PartitionServiceImpl(this);
   clusterService = new ClusterServiceImpl(this);
   textCommandService = new TextCommandServiceImpl(this);
   initializer.printNodeInfo(this);
   buildNumber = initializer.getBuildNumber();
   VersionCheck.check(this, initializer.getBuild(), initializer.getVersion());
   JoinConfig join = config.getNetworkConfig().getJoin();
   MulticastService mcService = null;
   try {
     if (join.getMulticastConfig().isEnabled()) {
       MulticastConfig multicastConfig = join.getMulticastConfig();
       MulticastSocket multicastSocket = new MulticastSocket(null);
       multicastSocket.setReuseAddress(true);
       // bind to receive interface
       multicastSocket.bind(new InetSocketAddress(multicastConfig.getMulticastPort()));
       multicastSocket.setTimeToLive(multicastConfig.getMulticastTimeToLive());
       try {
         // set the send interface
         final Address bindAddress = addressPicker.getBindAddress();
         // http://bugs.sun.com/bugdatabase/view_bug.do?bug_id=4417033
         // http://bugs.sun.com/bugdatabase/view_bug.do?bug_id=6402758
         if (!bindAddress.getInetAddress().isLoopbackAddress()) {
           multicastSocket.setInterface(bindAddress.getInetAddress());
         }
       } catch (Exception e) {
         logger.warning(e);
       }
       multicastSocket.setReceiveBufferSize(64 * 1024);
       multicastSocket.setSendBufferSize(64 * 1024);
       String multicastGroup = System.getProperty("hazelcast.multicast.group");
       if (multicastGroup == null) {
         multicastGroup = multicastConfig.getMulticastGroup();
       }
       multicastConfig.setMulticastGroup(multicastGroup);
       multicastSocket.joinGroup(InetAddress.getByName(multicastGroup));
       multicastSocket.setSoTimeout(1000);
       mcService = new MulticastService(this, multicastSocket);
       mcService.addMulticastListener(new NodeMulticastListener(this));
     }
   } catch (Exception e) {
     logger.severe(e);
   }
   this.multicastService = mcService;
   initializeListeners(config);
   joiner = nodeContext.createJoiner(this);
 }