narendly commented on a change in pull request #788: Implement request forwarding for ZkRoutingDataWriter URL: https://github.com/apache/helix/pull/788#discussion_r384317288
########## File path: helix-rest/src/main/java/org/apache/helix/rest/server/resources/mock/MockMetadataStoreDirectoryAccessor.java ########## @@ -0,0 +1,118 @@ +package org.apache.helix.rest.server.resources.mock; + +/* + * 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 javax.ws.rs.Path; + +import org.apache.helix.msdcommon.datamodel.TrieRoutingData; +import org.apache.helix.msdcommon.exception.InvalidRoutingDataException; +import org.apache.helix.rest.metadatastore.MetadataStoreDirectory; +import org.apache.helix.rest.metadatastore.ZkMetadataStoreDirectory; +import org.apache.helix.rest.metadatastore.accessor.MetadataStoreRoutingDataReader; +import org.apache.helix.rest.metadatastore.accessor.MetadataStoreRoutingDataWriter; +import org.apache.helix.rest.metadatastore.accessor.ZkRoutingDataReader; +import org.apache.helix.rest.metadatastore.accessor.ZkRoutingDataWriter; +import org.apache.helix.rest.server.resources.metadatastore.MetadataStoreDirectoryAccessor; + + +/** + * An accessor that mocks the MetadataStoreDirectoryAccessor for testing purpose. + */ +@Path("/mock") +public class MockMetadataStoreDirectoryAccessor extends MetadataStoreDirectoryAccessor { + // A flag that will be modified if the underlying MockZkRoutingDataWriter makes an operation + // against ZooKeeper + public static boolean operatedOnZk = false; + // The instance of mockMSD that's created by this accessor; it's saved here to be closed later + public static MetadataStoreDirectory _mockMSDInstance; + + /** + * This method is overriden so that an instance of MockZkMetadataStoreDirectory can be passed in + */ + @Override + protected void buildMetadataStoreDirectory(String namespace, String address) { + try { + _metadataStoreDirectory = new MockZkMetadataStoreDirectory(namespace, address); + _mockMSDInstance = _metadataStoreDirectory; + } catch (InvalidRoutingDataException e) { + e.printStackTrace(); + } + } + + /** + * Used to artificially create another instance of ZkMetadataStoreDirectory. + * ZkMetadataStoreDirectory being a singleton makes it impossible to test it, + * therefore this is the only way to create another instance. + */ + class MockZkMetadataStoreDirectory extends ZkMetadataStoreDirectory { + MockZkMetadataStoreDirectory(String namespace, String zkAddress) + throws InvalidRoutingDataException { + super(); + + // Manually populate the map so that MockZkRoutingDataWriter can be passed in + _routingZkAddressMap.put(namespace, zkAddress); + _routingDataReaderMap.put(namespace, new ZkRoutingDataReader(namespace, zkAddress, this)); + _routingDataWriterMap.put(namespace, new MockZkRoutingDataWriter(namespace, zkAddress)); + _realmToShardingKeysMap.put(namespace, _routingDataReaderMap.get(namespace).getRoutingData()); + _routingDataMap.put(namespace, new TrieRoutingData(_realmToShardingKeysMap.get(namespace))); + } + + @Override + public void close() { + _routingDataReaderMap.values().forEach(MetadataStoreRoutingDataReader::close); + _routingDataWriterMap.values().forEach(MetadataStoreRoutingDataWriter::close); Review comment: Nit: Should you clear the maps? ---------------------------------------------------------------- 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]
