Search in sources :

Example 1 with Timer

use of org.apache.hadoop.util.Timer in project hadoop by apache.

the class TestAccessTokenTimer method shouldRefreshIsCorrect.

@Test
public void shouldRefreshIsCorrect() {
    Timer mockTimer = mock(Timer.class);
    when(mockTimer.now()).thenReturn(500l).thenReturn(1000000l + 500l);
    AccessTokenTimer timer = new AccessTokenTimer(mockTimer);
    timer.setExpiresInMSSinceEpoch("1000000");
    assertFalse(timer.shouldRefresh());
    assertTrue(timer.shouldRefresh());
    verify(mockTimer, times(2)).now();
}
Also used : Timer(org.apache.hadoop.util.Timer) Test(org.junit.Test)

Example 2 with Timer

use of org.apache.hadoop.util.Timer in project hadoop by apache.

the class TestRefreshTokenTimeBasedTokenRefresher method refreshUrlIsCorrect.

@Test
public void refreshUrlIsCorrect() throws IOException {
    final int PORT = 7552;
    final String REFRESH_ADDRESS = "http://localhost:" + PORT + "/refresh";
    long tokenExpires = 0;
    Configuration conf = buildConf("refresh token key", Long.toString(tokenExpires), "joebob", REFRESH_ADDRESS);
    Timer mockTimer = mock(Timer.class);
    when(mockTimer.now()).thenReturn(tokenExpires + 1000l);
    AccessTokenProvider tokenProvider = new ConfRefreshTokenBasedAccessTokenProvider(mockTimer);
    tokenProvider.setConf(conf);
    // Build mock server to receive refresh request
    ClientAndServer mockServer = startClientAndServer(PORT);
    HttpRequest expectedRequest = request().withMethod("POST").withPath("/refresh").withBody(ParameterBody.params(Parameter.param(CLIENT_ID, "joebob"), Parameter.param(GRANT_TYPE, REFRESH_TOKEN), Parameter.param(REFRESH_TOKEN, "refresh token key")));
    MockServerClient mockServerClient = new MockServerClient("localhost", PORT);
    // https://tools.ietf.org/html/rfc6749#section-5.1
    Map<String, Object> map = new TreeMap<>();
    map.put(EXPIRES_IN, "0987654321");
    map.put(TOKEN_TYPE, BEARER);
    map.put(ACCESS_TOKEN, "new access token");
    ObjectMapper mapper = new ObjectMapper();
    HttpResponse resp = response().withStatusCode(HttpStatus.SC_OK).withHeaders(CONTENT_TYPE_APPLICATION_JSON).withBody(mapper.writeValueAsString(map));
    mockServerClient.when(expectedRequest, exactly(1)).respond(resp);
    assertEquals("new access token", tokenProvider.getAccessToken());
    mockServerClient.verify(expectedRequest);
    mockServerClient.clear(expectedRequest);
    mockServer.stop();
}
Also used : HttpRequest(org.mockserver.model.HttpRequest) Configuration(org.apache.hadoop.conf.Configuration) HttpResponse(org.mockserver.model.HttpResponse) MockServerClient(org.mockserver.client.server.MockServerClient) TreeMap(java.util.TreeMap) Timer(org.apache.hadoop.util.Timer) ClientAndServer(org.mockserver.integration.ClientAndServer) ClientAndServer.startClientAndServer(org.mockserver.integration.ClientAndServer.startClientAndServer) ObjectMapper(com.fasterxml.jackson.databind.ObjectMapper) Test(org.junit.Test)

Example 3 with Timer

use of org.apache.hadoop.util.Timer in project hadoop by apache.

the class FsVolumeImpl method addBlockPool.

void addBlockPool(String bpid, Configuration c, Timer timer) throws IOException {
    File bpdir = new File(currentDir, bpid);
    BlockPoolSlice bp;
    if (timer == null) {
        bp = new BlockPoolSlice(bpid, this, bpdir, c, new Timer());
    } else {
        bp = new BlockPoolSlice(bpid, this, bpdir, c, timer);
    }
    bpSlices.put(bpid, bp);
}
Also used : Timer(org.apache.hadoop.util.Timer) RandomAccessFile(java.io.RandomAccessFile) File(java.io.File)

Example 4 with Timer

use of org.apache.hadoop.util.Timer in project hadoop by apache.

the class DataNode method makeInstance.

/**
   * Make an instance of DataNode after ensuring that at least one of the
   * given data directories (and their parent directories, if necessary)
   * can be created.
   * @param dataDirs List of directories, where the new DataNode instance should
   * keep its files.
   * @param conf Configuration instance to use.
   * @param resources Secure resources needed to run under Kerberos
   * @return DataNode instance for given list of data dirs and conf, or null if
   * no directory from this directory list can be created.
   * @throws IOException
   */
static DataNode makeInstance(Collection<StorageLocation> dataDirs, Configuration conf, SecureResources resources) throws IOException {
    List<StorageLocation> locations;
    StorageLocationChecker storageLocationChecker = new StorageLocationChecker(conf, new Timer());
    try {
        locations = storageLocationChecker.check(conf, dataDirs);
    } catch (InterruptedException ie) {
        throw new IOException("Failed to instantiate DataNode", ie);
    }
    DefaultMetricsSystem.initialize("DataNode");
    assert locations.size() > 0 : "number of data directories should be > 0";
    return new DataNode(conf, locations, storageLocationChecker, resources);
}
Also used : Timer(org.apache.hadoop.util.Timer) StorageLocationChecker(org.apache.hadoop.hdfs.server.datanode.checker.StorageLocationChecker) IOException(java.io.IOException)

Example 5 with Timer

use of org.apache.hadoop.util.Timer in project hadoop by apache.

the class TestAccessTokenTimer method expireConversionWorks.

@Test
public void expireConversionWorks() {
    Timer mockTimer = mock(Timer.class);
    when(mockTimer.now()).thenReturn(5l);
    AccessTokenTimer timer = new AccessTokenTimer(mockTimer);
    timer.setExpiresIn("3");
    assertEquals(3005, timer.getNextRefreshMSSinceEpoch());
    assertTrue(timer.shouldRefresh());
}
Also used : Timer(org.apache.hadoop.util.Timer) Test(org.junit.Test)

Aggregations

Timer (org.apache.hadoop.util.Timer)6 Test (org.junit.Test)4 ObjectMapper (com.fasterxml.jackson.databind.ObjectMapper)2 TreeMap (java.util.TreeMap)2 Configuration (org.apache.hadoop.conf.Configuration)2 MockServerClient (org.mockserver.client.server.MockServerClient)2 ClientAndServer (org.mockserver.integration.ClientAndServer)2 ClientAndServer.startClientAndServer (org.mockserver.integration.ClientAndServer.startClientAndServer)2 HttpRequest (org.mockserver.model.HttpRequest)2 HttpResponse (org.mockserver.model.HttpResponse)2 File (java.io.File)1 IOException (java.io.IOException)1 RandomAccessFile (java.io.RandomAccessFile)1 StorageLocationChecker (org.apache.hadoop.hdfs.server.datanode.checker.StorageLocationChecker)1