Search in sources :

Example 1 with MockMetadataStoreDirectoryServer

use of org.apache.helix.msdcommon.mock.MockMetadataStoreDirectoryServer in project helix by apache.

the class TestMultiZkConectionConfig method beforeClass.

@BeforeClass
public void beforeClass() throws Exception {
    // Create 3 in-memory zookeepers and routing mapping
    for (int i = 0; i < NUM_ZK; i++) {
        String zkAddress = ZK_PREFIX + (ZK_START_PORT + i);
        ZK_SERVER_MAP.put(zkAddress, TestHelper.startZkServer(zkAddress));
        ZK_CLIENT_MAP.put(zkAddress, DedicatedZkClientFactory.getInstance().buildZkClient(new HelixZkClient.ZkConnectionConfig(zkAddress), new HelixZkClient.ZkClientConfig().setZkSerializer(new ZNRecordSerializer())));
        // One cluster per ZkServer created
        _rawRoutingData.put(zkAddress, Collections.singletonList("/" + CLUSTER_LIST.get(i)));
    }
    // Create a Mock MSDS
    final String msdsHostName = "localhost";
    final int msdsPort = 11117;
    final String msdsNamespace = "multiZkTest";
    _msdsEndpoint = "http://" + msdsHostName + ":" + msdsPort + "/admin/v2/namespaces/" + msdsNamespace;
    _msds = new MockMetadataStoreDirectoryServer(msdsHostName, msdsPort, msdsNamespace, _rawRoutingData);
    _msds.startServer();
    // Save previously-set system configs
    String prevMultiZkEnabled = System.getProperty(SystemPropertyKeys.MULTI_ZK_ENABLED);
    String prevMsdsServerEndpoint = System.getProperty(MetadataStoreRoutingConstants.MSDS_SERVER_ENDPOINT_KEY);
    if (prevMultiZkEnabled != null) {
        _configStore.put(SystemPropertyKeys.MULTI_ZK_ENABLED, prevMultiZkEnabled);
    }
    if (prevMsdsServerEndpoint != null) {
        _configStore.put(MetadataStoreRoutingConstants.MSDS_SERVER_ENDPOINT_KEY, prevMsdsServerEndpoint);
    }
    // Turn on multiZk mode in System config
    System.setProperty(SystemPropertyKeys.MULTI_ZK_ENABLED, "true");
    // MSDS endpoint: http://localhost:11117/admin/v2/namespaces/multiZkTest
    // We are not setting routing ZK in system property.
    // System.setProperty(MetadataStoreRoutingConstants.MSDS_SERVER_ENDPOINT_KEY, _msdsEndpoint);
    // Routing data may be set by other tests using the same endpoint; reset() for good measure
    RoutingDataManager.getInstance().reset();
    try {
        _zkClient = new FederatedZkClient(new RealmAwareZkClient.RealmAwareZkConnectionConfig.Builder().setRoutingDataSourceEndpoint(_msdsEndpoint + "," + ZK_PREFIX + ZK_START_PORT).setRoutingDataSourceType(RoutingDataReaderType.HTTP_ZK_FALLBACK.name()).build(), new RealmAwareZkClient.RealmAwareZkClientConfig());
        _zkClient.setZkSerializer(new ZNRecordSerializer());
    } catch (Exception ex) {
        for (StackTraceElement elm : ex.getStackTrace()) {
            System.out.println(elm);
        }
    }
    System.out.println("end start");
}
Also used : HelixZkClient(org.apache.helix.zookeeper.api.client.HelixZkClient) MockMetadataStoreDirectoryServer(org.apache.helix.msdcommon.mock.MockMetadataStoreDirectoryServer) HelixException(org.apache.helix.HelixException) FederatedZkClient(org.apache.helix.zookeeper.impl.client.FederatedZkClient) ZNRecordSerializer(org.apache.helix.zookeeper.datamodel.serializer.ZNRecordSerializer) BeforeClass(org.testng.annotations.BeforeClass)

Example 2 with MockMetadataStoreDirectoryServer

use of org.apache.helix.msdcommon.mock.MockMetadataStoreDirectoryServer in project helix by apache.

the class TestMultiZkHelixJavaApis method testDifferentMsdsEndpointConfigs.

/**
 * Tests Helix Java APIs which use different MSDS endpoint configs. Java API should
 * only connect to the configured MSDS but not the others. The APIs are explicitly tested are:
 * - ClusterSetup
 * - HelixAdmin
 * - ZkUtil
 * - HelixManager
 * - BaseDataAccessor
 * - ConfigAccessor
 */
@Test(dependsOnMethods = "testGenericBaseDataAccessorBuilder")
public void testDifferentMsdsEndpointConfigs() throws IOException, InvalidRoutingDataException {
    String methodName = TestHelper.getTestMethodName();
    System.out.println("Start " + methodName);
    final String zkAddress = ZK_SERVER_MAP.keySet().iterator().next();
    final Map<String, Collection<String>> secondRoutingData = ImmutableMap.of(zkAddress, Collections.singletonList(formPath(CLUSTER_FOUR)));
    MockMetadataStoreDirectoryServer secondMsds = new MockMetadataStoreDirectoryServer("localhost", 11118, "multiZkTest", secondRoutingData);
    final RealmAwareZkClient.RealmAwareZkConnectionConfig connectionConfig = new RealmAwareZkClient.RealmAwareZkConnectionConfig.Builder().setRoutingDataSourceType(RoutingDataReaderType.HTTP.name()).setRoutingDataSourceEndpoint(secondMsds.getEndpoint()).build();
    secondMsds.startServer();
    try {
        // Verify ClusterSetup
        verifyClusterSetupMsdsEndpoint(connectionConfig);
        // Verify HelixAdmin
        verifyHelixAdminMsdsEndpoint(connectionConfig);
        // Verify ZKUtil
        verifyZkUtilMsdsEndpoint();
        // Verify HelixManager
        verifyHelixManagerMsdsEndpoint();
        // Verify BaseDataAccessor
        verifyBaseDataAccessorMsdsEndpoint(connectionConfig);
        // Verify ConfigAccessor
        verifyConfigAccessorMsdsEndpoint(connectionConfig);
    } finally {
        RealmAwareZkClient zkClient = new FederatedZkClient(connectionConfig, new RealmAwareZkClient.RealmAwareZkClientConfig());
        TestHelper.dropCluster(CLUSTER_FOUR, zkClient);
        zkClient.close();
        secondMsds.stopServer();
    }
    System.out.println("End " + methodName);
}
Also used : FederatedZkClient(org.apache.helix.zookeeper.impl.client.FederatedZkClient) Collection(java.util.Collection) MockMetadataStoreDirectoryServer(org.apache.helix.msdcommon.mock.MockMetadataStoreDirectoryServer) RealmAwareZkClient(org.apache.helix.zookeeper.api.client.RealmAwareZkClient) Test(org.testng.annotations.Test)

Example 3 with MockMetadataStoreDirectoryServer

use of org.apache.helix.msdcommon.mock.MockMetadataStoreDirectoryServer in project helix by apache.

the class TestFederatedZkClient method testUpdateRoutingDataOnCacheMissMSDS.

/**
 * This tests the routing data update feature only enabled when
 * RoutingSystemPropertyKeys.UPDATE_ROUTING_DATA_ON_CACHE_MISS is set to true.
 * Routing data source is MSDS.
 */
@Test(dependsOnMethods = "testMultiRealmCRUD")
public void testUpdateRoutingDataOnCacheMissMSDS() throws IOException, InvalidRoutingDataException {
    // Enable routing data update upon cache miss
    System.setProperty(RoutingSystemPropertyKeys.UPDATE_ROUTING_DATA_ON_CACHE_MISS, "true");
    // Set the routing data update interval to 0 so there's no delay in testing
    System.setProperty(RoutingSystemPropertyKeys.ROUTING_DATA_UPDATE_INTERVAL_MS, "0");
    RoutingDataManager.getInstance().getMetadataStoreRoutingData();
    _msdsServer.stopServer();
    /*
     * Test is 2-tiered because cache update is 2-tiered
     * Case 1:
     * - RoutingDataManager (in-memory) does not have the key
     * - MSDS has the key
     * This simulates a case where FederatedZkClient must do a I/O based update.
     */
    // Start MSDS with a new key
    String newShardingKey = "/sharding-key-9";
    String zkRealm = "localhost:2127";
    Map<String, Collection<String>> rawRoutingData = new HashMap<>();
    rawRoutingData.put(zkRealm, new ArrayList<>());
    // Add a new key
    rawRoutingData.get(zkRealm).add(newShardingKey);
    _msdsServer = new MockMetadataStoreDirectoryServer(MSDS_HOSTNAME, MSDS_PORT, MSDS_NAMESPACE, rawRoutingData);
    _msdsServer.startServer();
    // Verify that RoutingDataManager does not have the key
    MetadataStoreRoutingData routingData = RoutingDataManager.getInstance().getMetadataStoreRoutingData();
    try {
        routingData.getMetadataStoreRealm(newShardingKey);
        Assert.fail("Must throw NoSuchElementException!");
    } catch (NoSuchElementException e) {
    // Expected
    }
    // Create a new FederatedZkClient
    FederatedZkClient federatedZkClient = new FederatedZkClient(new RealmAwareZkClient.RealmAwareZkConnectionConfig.Builder().setRoutingDataSourceType(RoutingDataReaderType.HTTP.name()).setRoutingDataSourceEndpoint("http://" + MSDS_HOSTNAME + ":" + MSDS_PORT + "/admin/v2/namespaces/" + MSDS_NAMESPACE).build(), new RealmAwareZkClient.RealmAwareZkClientConfig());
    // exists() must succeed and RoutingDataManager should now have the key (cache update must have
    // happened)
    // False expected for the following call because the znode does not exist and we are checking
    // whether the call succeeds or not
    Assert.assertFalse(federatedZkClient.exists(newShardingKey));
    Assert.assertEquals(zkRealm, RoutingDataManager.getInstance().getMetadataStoreRoutingData().getMetadataStoreRealm(newShardingKey));
    /*
     * Case 2:
     * - RoutingDataManager has the key
     * - MSDS does not have the key
     * - continue using the same ZkClient because we want an existing federated client that does
     * not have the key
     */
    _msdsServer.stopServer();
    // Create an MSDS with the key and reset MSDS so it doesn't contain the key
    String newShardingKey2 = "/sharding-key-10";
    rawRoutingData.get(zkRealm).add(newShardingKey2);
    _msdsServer = new MockMetadataStoreDirectoryServer(MSDS_HOSTNAME, MSDS_PORT, MSDS_NAMESPACE, rawRoutingData);
    _msdsServer.startServer();
    // Make sure RoutingDataManager has the key
    RoutingDataManager.getInstance().reset();
    Assert.assertEquals(zkRealm, RoutingDataManager.getInstance().getMetadataStoreRoutingData().getMetadataStoreRealm(newShardingKey2));
    // Reset MSDS so it doesn't contain the key
    _msdsServer.stopServer();
    _msdsServer = new MockMetadataStoreDirectoryServer(MSDS_HOSTNAME, MSDS_PORT, MSDS_NAMESPACE, // FAKE_ROUTING_DATA doesn't contain the key
    TestConstants.FAKE_ROUTING_DATA);
    _msdsServer.startServer();
    // exists() must succeed and RoutingDataManager should still have the key
    // This means that we do not do a hard update (I/O based update) because in-memory cache already
    // has the key
    // False expected for the following call because the znode does not exist and we are checking
    // whether the call succeeds or not
    Assert.assertFalse(federatedZkClient.exists(newShardingKey2));
    Assert.assertEquals(zkRealm, RoutingDataManager.getInstance().getMetadataStoreRoutingData().getMetadataStoreRealm(newShardingKey2));
    // Also check that MSDS does not have the new sharding key through resetting RoutingDataManager
    // and re-reading from MSDS
    RoutingDataManager.getInstance().reset();
    try {
        RoutingDataManager.getInstance().getMetadataStoreRoutingData().getMetadataStoreRealm(newShardingKey2);
        Assert.fail("NoSuchElementException expected!");
    } catch (NoSuchElementException e) {
    // Expected because MSDS does not contain the key
    }
    // Clean up federatedZkClient
    federatedZkClient.close();
    // Shut down MSDS
    _msdsServer.stopServer();
    // Disable System property
    System.clearProperty(RoutingSystemPropertyKeys.UPDATE_ROUTING_DATA_ON_CACHE_MISS);
    System.clearProperty(RoutingSystemPropertyKeys.ROUTING_DATA_UPDATE_INTERVAL_MS);
}
Also used : MetadataStoreRoutingData(org.apache.helix.msdcommon.datamodel.MetadataStoreRoutingData) HashMap(java.util.HashMap) Collection(java.util.Collection) MockMetadataStoreDirectoryServer(org.apache.helix.msdcommon.mock.MockMetadataStoreDirectoryServer) NoSuchElementException(java.util.NoSuchElementException) RealmAwareZkClient(org.apache.helix.zookeeper.api.client.RealmAwareZkClient) Test(org.testng.annotations.Test)

Example 4 with MockMetadataStoreDirectoryServer

use of org.apache.helix.msdcommon.mock.MockMetadataStoreDirectoryServer in project helix by apache.

the class TestHttpZkFallbackRoutingDataReader method beforeClass.

@BeforeClass
public void beforeClass() throws IOException {
    // Start MockMSDS
    _msdsServer = new MockMetadataStoreDirectoryServer(HOST, PORT, NAMESPACE, TestConstants.FAKE_ROUTING_DATA);
    _msdsServer.startServer();
}
Also used : MockMetadataStoreDirectoryServer(org.apache.helix.msdcommon.mock.MockMetadataStoreDirectoryServer) BeforeClass(org.testng.annotations.BeforeClass)

Example 5 with MockMetadataStoreDirectoryServer

use of org.apache.helix.msdcommon.mock.MockMetadataStoreDirectoryServer in project helix by apache.

the class TestRoutingDataManager method beforeClass.

@BeforeClass
public void beforeClass() throws IOException {
    // Start MockMSDS
    _msdsServer = new MockMetadataStoreDirectoryServer(HOST, PORT, NAMESPACE, TestConstants.FAKE_ROUTING_DATA);
    _msdsServer.startServer();
    // Register the endpoint as a System property
    System.setProperty(MetadataStoreRoutingConstants.MSDS_SERVER_ENDPOINT_KEY, MSDS_ENDPOINT);
    // Reset RoutingDataManager
    RoutingDataManager.getInstance().reset();
}
Also used : MockMetadataStoreDirectoryServer(org.apache.helix.msdcommon.mock.MockMetadataStoreDirectoryServer) BeforeClass(org.testng.annotations.BeforeClass)

Aggregations

MockMetadataStoreDirectoryServer (org.apache.helix.msdcommon.mock.MockMetadataStoreDirectoryServer)9 BeforeClass (org.testng.annotations.BeforeClass)5 Collection (java.util.Collection)4 Test (org.testng.annotations.Test)4 HashMap (java.util.HashMap)3 RealmAwareZkClient (org.apache.helix.zookeeper.api.client.RealmAwareZkClient)3 FederatedZkClient (org.apache.helix.zookeeper.impl.client.FederatedZkClient)3 MetadataStoreRoutingData (org.apache.helix.msdcommon.datamodel.MetadataStoreRoutingData)2 HelixZkClient (org.apache.helix.zookeeper.api.client.HelixZkClient)2 ZNRecordSerializer (org.apache.helix.zookeeper.datamodel.serializer.ZNRecordSerializer)2 ImmutableSet (com.google.common.collect.ImmutableSet)1 HashSet (java.util.HashSet)1 List (java.util.List)1 Map (java.util.Map)1 NoSuchElementException (java.util.NoSuchElementException)1 Set (java.util.Set)1 HelixException (org.apache.helix.HelixException)1 MockParticipantManager (org.apache.helix.integration.manager.MockParticipantManager)1 ZKHelixManager (org.apache.helix.manager.zk.ZKHelixManager)1 InstanceConfig (org.apache.helix.model.InstanceConfig)1