Search in sources :

Example 11 with Time

use of com.github.ambry.utils.Time in project ambry by linkedin.

the class AmbryUrlSigningServiceTest method signAndVerifyTest.

/**
 * Tests that generate and verify signed URLs.
 * @throws Exception
 */
@Test
public void signAndVerifyTest() throws Exception {
    Time time = new MockTime();
    AmbryUrlSigningService signer = getUrlSignerWithDefaults(time);
    doSignAndVerifyTest(signer, RestMethod.POST, time);
    doSignAndVerifyTest(signer, RestMethod.GET, time);
    signFailuresTest();
}
Also used : MockTime(com.github.ambry.utils.MockTime) Time(com.github.ambry.utils.Time) MockTime(com.github.ambry.utils.MockTime) Test(org.junit.Test)

Example 12 with Time

use of com.github.ambry.utils.Time in project ambry by linkedin.

the class MockSelector method testRequestDropping.

/**
 * Test dropping of requests by closing
 */
@Test
public void testRequestDropping() {
    AtomicInteger nextCorrelationId = new AtomicInteger(1);
    Function<Integer, List<RequestInfo>> requestGen = numRequests -> IntStream.range(0, numRequests).mapToObj(i -> new RequestInfo(sslHost, sslPort, new MockSend(nextCorrelationId.getAndIncrement()), replicaOnSslNode, null)).collect(Collectors.toList());
    List<ResponseInfo> responseInfoList;
    // Drop requests while the requests are waiting for a connection.
    // First poll will require connections to be created, so no responses will be returned.
    responseInfoList = networkClient.sendAndPoll(requestGen.apply(3), Collections.emptySet(), POLL_TIMEOUT_MS);
    Assert.assertEquals("No responses expected in first poll.", 0, responseInfoList.size());
    // Drop requests on the second poll. The requests should be removed from the pending request list as a result.
    responseInfoList = networkClient.sendAndPoll(Collections.emptyList(), new HashSet<>(Arrays.asList(2, 3)), POLL_TIMEOUT_MS);
    Assert.assertEquals("Should receive only as many responses as there were requests", 3, responseInfoList.size());
    for (ResponseInfo responseInfo : responseInfoList) {
        MockSend send = (MockSend) responseInfo.getRequestInfo().getRequest();
        if (send.getCorrelationId() == 1) {
            NetworkClientErrorCode error = responseInfo.getError();
            ByteBuf response = responseInfo.content();
            Assert.assertNull("Should not have encountered an error", error);
            Assert.assertNotNull("Should receive a valid response", response);
            int correlationIdInRequest = send.getCorrelationId();
            int correlationIdInResponse = response.readInt();
            Assert.assertEquals("Received response for the wrong request", correlationIdInRequest, correlationIdInResponse);
        } else {
            Assert.assertEquals("Expected connection unavailable on dropped request", NetworkClientErrorCode.ConnectionUnavailable, responseInfo.getError());
            Assert.assertNull("Should not receive a response", responseInfo.content());
        }
    }
    responseInfoList.forEach(ResponseInfo::release);
    // Test dropping of requests while the requests are in flight.
    // Set the selector to idle mode to prevent responses from coming back (even though connections are available at
    // this moment in time).
    selector.setState(MockSelectorState.IdlePoll);
    responseInfoList = networkClient.sendAndPoll(requestGen.apply(3), Collections.emptySet(), POLL_TIMEOUT_MS);
    Assert.assertEquals("No responses expected in idle poll.", 0, responseInfoList.size());
    // Set the selector back to normal mode and drop a request. It should be dropped by closing the connection.
    selector.setState(MockSelectorState.Good);
    responseInfoList = networkClient.sendAndPoll(Collections.emptyList(), Collections.singleton(4), POLL_TIMEOUT_MS);
    Assert.assertEquals("Should receive only as many responses as there were requests", 3, responseInfoList.size());
    for (ResponseInfo responseInfo : responseInfoList) {
        MockSend send = (MockSend) responseInfo.getRequestInfo().getRequest();
        if (send.getCorrelationId() != 4) {
            NetworkClientErrorCode error = responseInfo.getError();
            ByteBuf response = responseInfo.content();
            Assert.assertNull("Should not have encountered an error", error);
            Assert.assertNotNull("Should receive a valid response", response);
            int correlationIdInRequest = send.getCorrelationId();
            int correlationIdInResponse = response.readInt();
            Assert.assertEquals("Received response for the wrong request", correlationIdInRequest, correlationIdInResponse);
            responseInfo.release();
        } else {
            Assert.assertEquals("Expected network error (from closed connection for dropped request)", NetworkClientErrorCode.NetworkError, responseInfo.getError());
            Assert.assertNull("Should not receive a response", responseInfo.content());
            responseInfo.release();
        }
    }
    // Dropping a request that is not currently pending or in flight should be a no-op.
    responseInfoList = networkClient.sendAndPoll(Collections.emptyList(), Collections.singleton(1), POLL_TIMEOUT_MS);
    Assert.assertEquals("No more responses expected.", 0, responseInfoList.size());
    responseInfoList.forEach(ResponseInfo::release);
}
Also used : IntStream(java.util.stream.IntStream) Arrays(java.util.Arrays) ByteBufferChannel(com.github.ambry.utils.ByteBufferChannel) DataNodeId(com.github.ambry.clustermap.DataNodeId) Function(java.util.function.Function) ByteBuffer(java.nio.ByteBuffer) ArrayList(java.util.ArrayList) HashSet(java.util.HashSet) ByteBuf(io.netty.buffer.ByteBuf) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) After(org.junit.After) NetworkConfig(com.github.ambry.config.NetworkConfig) NettyByteBufLeakHelper(com.github.ambry.utils.NettyByteBufLeakHelper) Time(com.github.ambry.utils.Time) Before(org.junit.Before) MockDataNodeId(com.github.ambry.clustermap.MockDataNodeId) MetricRegistry(com.codahale.metrics.MetricRegistry) Properties(java.util.Properties) VerifiableProperties(com.github.ambry.config.VerifiableProperties) Set(java.util.Set) IOException(java.io.IOException) Test(org.junit.Test) PooledByteBufAllocator(io.netty.buffer.PooledByteBufAllocator) InetSocketAddress(java.net.InetSocketAddress) Collectors(java.util.stream.Collectors) AbstractByteBufHolder(com.github.ambry.utils.AbstractByteBufHolder) List(java.util.List) MockTime(com.github.ambry.utils.MockTime) ReplicaId(com.github.ambry.clustermap.ReplicaId) WritableByteChannel(java.nio.channels.WritableByteChannel) Assert(org.junit.Assert) Collections(java.util.Collections) MockClusterMap(com.github.ambry.clustermap.MockClusterMap) ByteBuf(io.netty.buffer.ByteBuf) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) ArrayList(java.util.ArrayList) List(java.util.List) HashSet(java.util.HashSet) Test(org.junit.Test)

Example 13 with Time

use of com.github.ambry.utils.Time in project ambry by linkedin.

the class ReplicationTest method replicaTokenTest.

/**
 * Tests that replica tokens are set correctly and go through different stages correctly.
 * @throws InterruptedException
 */
@Test
public void replicaTokenTest() throws InterruptedException {
    final long tokenPersistInterval = 100;
    Time time = new MockTime();
    MockFindToken token1 = new MockFindToken(0, 0);
    RemoteReplicaInfo remoteReplicaInfo = new RemoteReplicaInfo(new MockReplicaId(ReplicaType.DISK_BACKED), new MockReplicaId(ReplicaType.DISK_BACKED), new InMemoryStore(null, Collections.emptyList(), Collections.emptyList(), null), token1, tokenPersistInterval, time, new Port(5000, PortType.PLAINTEXT));
    // The equality check is for the reference, which is fine.
    // Initially, the current token and the token to persist are the same.
    assertEquals(token1, remoteReplicaInfo.getToken());
    assertEquals(token1, remoteReplicaInfo.getTokenToPersist());
    MockFindToken token2 = new MockFindToken(100, 100);
    remoteReplicaInfo.initializeTokens(token2);
    // Both tokens should be the newly initialized token.
    assertEquals(token2, remoteReplicaInfo.getToken());
    assertEquals(token2, remoteReplicaInfo.getTokenToPersist());
    remoteReplicaInfo.onTokenPersisted();
    MockFindToken token3 = new MockFindToken(200, 200);
    remoteReplicaInfo.setToken(token3);
    // Token to persist should still be the old token.
    assertEquals(token3, remoteReplicaInfo.getToken());
    assertEquals(token2, remoteReplicaInfo.getTokenToPersist());
    remoteReplicaInfo.onTokenPersisted();
    // Sleep for shorter than token persist interval.
    time.sleep(tokenPersistInterval - 1);
    // Token to persist should still be the old token.
    assertEquals(token3, remoteReplicaInfo.getToken());
    assertEquals(token2, remoteReplicaInfo.getTokenToPersist());
    remoteReplicaInfo.onTokenPersisted();
    MockFindToken token4 = new MockFindToken(200, 200);
    remoteReplicaInfo.setToken(token4);
    time.sleep(2);
    // Token to persist should be the most recent token as of currentTime - tokenToPersistInterval
    // which is token3 at this time.
    assertEquals(token4, remoteReplicaInfo.getToken());
    assertEquals(token3, remoteReplicaInfo.getTokenToPersist());
    remoteReplicaInfo.onTokenPersisted();
    time.sleep(tokenPersistInterval + 1);
    // The most recently set token as of currentTime - tokenToPersistInterval is token4
    assertEquals(token4, remoteReplicaInfo.getToken());
    assertEquals(token4, remoteReplicaInfo.getTokenToPersist());
    remoteReplicaInfo.onTokenPersisted();
}
Also used : Port(com.github.ambry.network.Port) MockReplicaId(com.github.ambry.clustermap.MockReplicaId) MockTime(com.github.ambry.utils.MockTime) Time(com.github.ambry.utils.Time) MockTime(com.github.ambry.utils.MockTime) Test(org.junit.Test)

Example 14 with Time

use of com.github.ambry.utils.Time in project ambry by linkedin.

the class DumpIndexTool method main.

public static void main(String[] args) throws Exception {
    final AtomicInteger exitCode = new AtomicInteger(0);
    VerifiableProperties verifiableProperties = ToolUtils.getVerifiableProperties(args);
    DumpIndexToolConfig config = new DumpIndexToolConfig(verifiableProperties);
    ClusterMapConfig clusterMapConfig = new ClusterMapConfig(verifiableProperties);
    try (ClusterMap clusterMap = ((ClusterAgentsFactory) Utils.getObj(clusterMapConfig.clusterMapClusterAgentsFactory, clusterMapConfig, config.hardwareLayoutFilePath, config.partitionLayoutFilePath)).getClusterMap()) {
        StoreConfig storeConfig = new StoreConfig(verifiableProperties);
        // this tool supports only blob IDs. It can become generic if StoreKeyFactory provides a deserFromString method.
        BlobIdFactory blobIdFactory = new BlobIdFactory(clusterMap);
        StoreToolsMetrics metrics = new StoreToolsMetrics(clusterMap.getMetricRegistry());
        StoreMetrics storeMetrics = new StoreMetrics("DumpIndexTool", clusterMap.getMetricRegistry());
        ServerConfig serverConfig = new ServerConfig(verifiableProperties);
        Time time = SystemTime.getInstance();
        Throttler throttler = new Throttler(config.indexEntriesToProcessPerSec, 1000, true, time);
        StoreKeyConverterFactory storeKeyConverterFactory = Utils.getObj(serverConfig.serverStoreKeyConverterFactory, verifiableProperties, clusterMap.getMetricRegistry());
        DumpIndexTool dumpIndexTool = new DumpIndexTool(blobIdFactory, storeConfig, time, metrics, storeMetrics, throttler, storeKeyConverterFactory.getStoreKeyConverter());
        Set<StoreKey> filterKeySet = new HashSet<>();
        for (String key : config.filterSet) {
            filterKeySet.add(new BlobId(key, clusterMap));
        }
        switch(config.typeOfOperation) {
            case DumpIndex:
                dumpIndex(dumpIndexTool, config.pathOfInput, filterKeySet);
                break;
            case DumpIndexSegment:
                dumpIndexSegment(dumpIndexTool, config.pathOfInput, filterKeySet);
                break;
            case VerifyIndex:
                IndexProcessingResults results = dumpIndexTool.processIndex(config.pathOfInput, filterKeySet, time.milliseconds(), config.detectDuplicatesAcrossKeys);
                exitCode.set(reportVerificationResults(config.pathOfInput, results, config.failIfCraftedIdsPresent));
                break;
            case VerifyDataNode:
                DataNodeId dataNodeId = clusterMap.getDataNodeId(config.hostname, config.port);
                if (dataNodeId == null) {
                    logger.error("No data node corresponding to {}:{}", config.hostname, config.port);
                } else {
                    Set<File> replicaDirs = clusterMap.getReplicaIds(dataNodeId).stream().map(replicaId -> new File(replicaId.getMountPath())).collect(Collectors.toSet());
                    Map<File, IndexProcessingResults> resultsByReplica = dumpIndexTool.processIndex(replicaDirs, filterKeySet, config.parallelism, config.detectDuplicatesAcrossKeys);
                    replicaDirs.removeAll(resultsByReplica.keySet());
                    if (replicaDirs.size() != 0) {
                        logger.error("Results obtained missing {}", replicaDirs);
                        exitCode.set(5);
                    } else {
                        resultsByReplica.forEach((replicaDir, result) -> exitCode.set(Math.max(exitCode.get(), reportVerificationResults(replicaDir, result, config.failIfCraftedIdsPresent))));
                    }
                }
                break;
            default:
                throw new IllegalArgumentException("Unrecognized operation: " + config.typeOfOperation);
        }
    }
    System.exit(exitCode.get());
}
Also used : Arrays(java.util.Arrays) Default(com.github.ambry.config.Default) DataNodeId(com.github.ambry.clustermap.DataNodeId) LoggerFactory(org.slf4j.LoggerFactory) HashMap(java.util.HashMap) ArrayList(java.util.ArrayList) ConcurrentMap(java.util.concurrent.ConcurrentMap) HashSet(java.util.HashSet) Future(java.util.concurrent.Future) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) Map(java.util.Map) SystemTime(com.github.ambry.utils.SystemTime) Time(com.github.ambry.utils.Time) EnumSet(java.util.EnumSet) ExecutorService(java.util.concurrent.ExecutorService) StoreConfig(com.github.ambry.config.StoreConfig) Logger(org.slf4j.Logger) ClusterAgentsFactory(com.github.ambry.clustermap.ClusterAgentsFactory) VerifiableProperties(com.github.ambry.config.VerifiableProperties) ConcurrentHashMap(java.util.concurrent.ConcurrentHashMap) Config(com.github.ambry.config.Config) Set(java.util.Set) ClusterMap(com.github.ambry.clustermap.ClusterMap) ServerConfig(com.github.ambry.config.ServerConfig) Utils(com.github.ambry.utils.Utils) IOException(java.io.IOException) BlobIdFactory(com.github.ambry.commons.BlobIdFactory) Collectors(java.util.stream.Collectors) File(java.io.File) Executors(java.util.concurrent.Executors) AtomicLong(java.util.concurrent.atomic.AtomicLong) ToolUtils(com.github.ambry.tools.util.ToolUtils) List(java.util.List) Throttler(com.github.ambry.utils.Throttler) TreeMap(java.util.TreeMap) ClusterMapConfig(com.github.ambry.config.ClusterMapConfig) Timer(com.codahale.metrics.Timer) Collections(java.util.Collections) BlobId(com.github.ambry.commons.BlobId) SortedMap(java.util.SortedMap) ClusterMap(com.github.ambry.clustermap.ClusterMap) SystemTime(com.github.ambry.utils.SystemTime) Time(com.github.ambry.utils.Time) ServerConfig(com.github.ambry.config.ServerConfig) ClusterAgentsFactory(com.github.ambry.clustermap.ClusterAgentsFactory) Throttler(com.github.ambry.utils.Throttler) HashSet(java.util.HashSet) VerifiableProperties(com.github.ambry.config.VerifiableProperties) ClusterMapConfig(com.github.ambry.config.ClusterMapConfig) BlobIdFactory(com.github.ambry.commons.BlobIdFactory) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) StoreConfig(com.github.ambry.config.StoreConfig) BlobId(com.github.ambry.commons.BlobId) DataNodeId(com.github.ambry.clustermap.DataNodeId) File(java.io.File)

Aggregations

Time (com.github.ambry.utils.Time)14 ArrayList (java.util.ArrayList)9 List (java.util.List)8 StoreConfig (com.github.ambry.config.StoreConfig)7 VerifiableProperties (com.github.ambry.config.VerifiableProperties)7 MockTime (com.github.ambry.utils.MockTime)7 SystemTime (com.github.ambry.utils.SystemTime)7 IOException (java.io.IOException)7 HashSet (java.util.HashSet)7 AtomicInteger (java.util.concurrent.atomic.AtomicInteger)7 Test (org.junit.Test)7 MetricRegistry (com.codahale.metrics.MetricRegistry)6 Utils (com.github.ambry.utils.Utils)6 Collections (java.util.Collections)6 Set (java.util.Set)6 Collectors (java.util.stream.Collectors)6 Pair (com.github.ambry.utils.Pair)5 Arrays (java.util.Arrays)5 HashMap (java.util.HashMap)5 Map (java.util.Map)5