narendly commented on a change in pull request #788: Implement request forwarding for ZkRoutingDataWriter URL: https://github.com/apache/helix/pull/788#discussion_r384320772
########## File path: helix-rest/src/test/java/org/apache/helix/rest/server/TestMSDADistributedLeaderElection.java ########## @@ -0,0 +1,225 @@ +package org.apache.helix.rest.server; + +/* + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you 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. + */ + +import java.io.IOException; +import java.util.ArrayList; +import java.util.Arrays; +import java.util.Collections; +import java.util.Comparator; +import java.util.List; +import java.util.Set; +import javax.ws.rs.core.Response; + +import org.apache.helix.msdcommon.constant.MetadataStoreRoutingConstants; +import org.apache.helix.msdcommon.exception.InvalidRoutingDataException; +import org.apache.helix.rest.common.ContextPropertyKeys; +import org.apache.helix.rest.common.HelixRestNamespace; +import org.apache.helix.rest.common.ServletType; +import org.apache.helix.rest.server.auditlog.AuditLogger; +import org.apache.helix.rest.server.filters.CORSFilter; +import org.apache.helix.rest.server.resources.mock.MockMetadataStoreDirectoryAccessor; +import org.apache.helix.zookeeper.api.client.HelixZkClient; +import org.apache.helix.zookeeper.datamodel.ZNRecord; +import org.apache.helix.zookeeper.datamodel.serializer.ZNRecordSerializer; +import org.apache.helix.zookeeper.impl.factory.DedicatedZkClientFactory; +import org.apache.http.HttpResponse; +import org.apache.http.client.methods.HttpDelete; +import org.apache.http.client.methods.HttpPut; +import org.apache.http.client.methods.HttpUriRequest; +import org.apache.http.impl.client.CloseableHttpClient; +import org.apache.http.impl.client.HttpClients; +import org.glassfish.jersey.server.ResourceConfig; +import org.testng.Assert; +import org.testng.annotations.AfterClass; +import org.testng.annotations.BeforeClass; +import org.testng.annotations.Test; + +import static org.apache.helix.msdcommon.constant.MetadataStoreRoutingConstants.LEADER_ELECTION_ZNODE; + + +public class TestMSDADistributedLeaderElection extends MetadataStoreDirectoryAccessorTestBase { + private final String MOCK_URL_PREFIX = "/mock"; + + private HelixRestServer _mockHelixRestServer; + private String _mockBaseUri; + private String _leaderBaseUri; + private CloseableHttpClient _httpClient; + private HelixZkClient _zkClient; + + @BeforeClass + public void beforeClass() throws Exception { + super.beforeClass(); + _leaderBaseUri = getBaseUri().toString(); + + // Start a second server for testing Distributed Leader Election for writes + _mockBaseUri = + getBaseUri().getScheme() + "://" + getBaseUri().getHost() + ":" + (getBaseUri().getPort() + + 1); + try { + List<HelixRestNamespace> namespaces = new ArrayList<>(); + // Add test namespace + namespaces.add(new HelixRestNamespace(TEST_NAMESPACE, + HelixRestNamespace.HelixMetadataStoreType.ZOOKEEPER, _zkAddrTestNS, false)); + _mockHelixRestServer = + new MockHelixRestServer(namespaces, getBaseUri().getPort() + 1, getBaseUri().getPath(), + Collections.singletonList(_auditLogger)); + _mockHelixRestServer.start(); + } catch (InterruptedException e) { + e.printStackTrace(); + } + + // Calling the original endpoint to create an instance of MetadataStoreDirectory in case + // it didn't exist yet. + get(TEST_NAMESPACE_URI_PREFIX + "/metadata-store-realms", null, + Response.Status.OK.getStatusCode(), true); + + // Set the new uri to be used in leader election + System.setProperty(MetadataStoreRoutingConstants.MSDS_SERVER_HOSTNAME_KEY, _mockBaseUri); + + // Start http client for testing + _httpClient = HttpClients.createDefault(); + + // Start zkclient to verify leader election behavior + _zkClient = DedicatedZkClientFactory.getInstance() + .buildZkClient(new HelixZkClient.ZkConnectionConfig(_zkAddrTestNS), + new HelixZkClient.ZkClientConfig().setZkSerializer(new ZNRecordSerializer())); + } + + @AfterClass + public void afterClass() throws Exception { + super.afterClass(); + MockMetadataStoreDirectoryAccessor._mockMSDInstance.close(); + _mockHelixRestServer.shutdown(); + _httpClient.close(); + _zkClient.close(); + } + + @Test + public void testAddMetadataStoreRealmRequestForwarding() + throws InvalidRoutingDataException, IOException { + Set<String> expectedRealmsSet = getAllMetadataStoreRealmsHelper(); + Assert.assertFalse(expectedRealmsSet.contains(TEST_REALM_3), + "Metadata store directory should not have realm: " + TEST_REALM_3); + sendRequestAndValidate("/metadata-store-realms/" + TEST_REALM_3, "put", + Response.Status.CREATED.getStatusCode()); + expectedRealmsSet.add(TEST_REALM_3); + Assert.assertEquals(getAllMetadataStoreRealmsHelper(), expectedRealmsSet); + MockMetadataStoreDirectoryAccessor._mockMSDInstance.close(); + } + + @Test(dependsOnMethods = "testAddMetadataStoreRealmRequestForwarding") + public void testDeleteMetadataStoreRealmRequestForwarding() + throws InvalidRoutingDataException, IOException { + Set<String> expectedRealmsSet = getAllMetadataStoreRealmsHelper(); + sendRequestAndValidate("/metadata-store-realms/" + TEST_REALM_3, "delete", Review comment: Use enum for "delete" ---------------------------------------------------------------- This is an automated message from the Apache Git Service. To respond to the message, please log on to GitHub and use the URL above to go to the specific comment. For queries about this service, please contact Infrastructure at: [email protected] With regards, Apache Git Services --------------------------------------------------------------------- To unsubscribe, e-mail: [email protected] For additional commands, e-mail: [email protected]
