@SuppressWarnings("unchecked") @Test public void testFragmenterCallExpiresAfterTimeout() throws Throwable { List<Fragment> fragmentList1 = new ArrayList<>(); List<Fragment> fragmentList2 = new ArrayList<>(); RequestContext context1 = new RequestContext(); context1.setTransactionId("XID-XYZ-123456"); context1.setSegmentId(0); RequestContext context2 = new RequestContext(); context2.setTransactionId("XID-XYZ-123456"); context2.setSegmentId(1); when(parser.parseRequest(headersFromRequest1, RequestType.FRAGMENTER)).thenReturn(context1); when(parser.parseRequest(headersFromRequest2, RequestType.FRAGMENTER)).thenReturn(context2); when(fragmenterFactory.getPlugin(context1)).thenReturn(fragmenter1); when(fragmenterFactory.getPlugin(context2)).thenReturn(fragmenter2); when(fragmenter1.getFragments()).thenReturn(fragmentList1); when(fragmenter2.getFragments()).thenReturn(fragmentList2); Response response1 = new FragmenterResource(parser, fragmenterFactory, fragmenterCacheFactory) .getFragments(servletContext, headersFromRequest1); fakeTicker.advanceTime(11 * 1000); Response response2 = new FragmenterResource(parser, fragmenterFactory, fragmenterCacheFactory) .getFragments(servletContext, headersFromRequest2); verify(fragmenter1, times(1)).getFragments(); verify(fragmenter2, times(1)).getFragments(); assertNotNull(response1); assertNotNull(response2); assertNotNull(response1.getEntity()); assertNotNull(response2.getEntity()); assertTrue(response1.getEntity() instanceof FragmentsResponse); assertTrue(response2.getEntity() instanceof FragmentsResponse); assertSame(fragmentList1, ((FragmentsResponse) response1.getEntity()).getFragments()); assertSame(fragmentList2, ((FragmentsResponse) response2.getEntity()).getFragments()); }
@GET @Path("getFragments") @Produces("application/json") public Response getFragments(@Context final ServletContext servletContext, @Context final HttpHeaders headers) throws Throwable { LOG.debug("Received FRAGMENTER call"); startTime = System.currentTimeMillis(); final RequestContext context = parseRequest(headers); final String path = context.getDataSource(); final String fragmenterCacheKey = getFragmenterCacheKey(context); LOG.debug("FRAGMENTER started for path \"{}\"", path); List<Fragment> fragments; if (Utilities.isFragmenterCacheEnabled()) { try { fragments = fragmenterCacheFactory.getCache() .get(fragmenterCacheKey, new Callable<List<Fragment>>() { @Override public List<Fragment> call() throws Exception { didThreadProcessFragmentCall = true; LOG.debug("Caching fragments for transactionId={} from segmentId={} with key={}", context.getTransactionId(), context.getSegmentId(), fragmenterCacheKey); return getFragments(context); } }); } catch (UncheckedExecutionException | ExecutionException e) { if (e.getCause() != null) throw e.getCause(); throw e; } if (!didThreadProcessFragmentCall) { logFragmentStatistics(Level.DEBUG, context, fragments); } } else { LOG.debug("Fragmenter cache is disabled"); fragments = getFragments(context); } FragmentsResponse fragmentsResponse = FragmentsResponseFormatter.formatResponse(fragments, path); return Response.ok(fragmentsResponse, MediaType.APPLICATION_JSON_TYPE).build(); }
FragmenterResource extends BaseResource { @GET @Path("getFragments") @Produces("application/json") public Response getFragments(@Context final ServletContext servletContext, @Context final HttpHeaders headers) throws Throwable { LOG.debug("Received FRAGMENTER call"); startTime = System.currentTimeMillis(); final RequestContext context = parseRequest(headers); final String path = context.getDataSource(); final String fragmenterCacheKey = getFragmenterCacheKey(context); LOG.debug("FRAGMENTER started for path \"{}\"", path); List<Fragment> fragments; if (Utilities.isFragmenterCacheEnabled()) { try { fragments = fragmenterCacheFactory.getCache() .get(fragmenterCacheKey, new Callable<List<Fragment>>() { @Override public List<Fragment> call() throws Exception { didThreadProcessFragmentCall = true; LOG.debug("Caching fragments for transactionId={} from segmentId={} with key={}", context.getTransactionId(), context.getSegmentId(), fragmenterCacheKey); return getFragments(context); } }); } catch (UncheckedExecutionException | ExecutionException e) { if (e.getCause() != null) throw e.getCause(); throw e; } if (!didThreadProcessFragmentCall) { logFragmentStatistics(Level.DEBUG, context, fragments); } } else { LOG.debug("Fragmenter cache is disabled"); fragments = getFragments(context); } FragmentsResponse fragmentsResponse = FragmentsResponseFormatter.formatResponse(fragments, path); return Response.ok(fragmentsResponse, MediaType.APPLICATION_JSON_TYPE).build(); } }
FragmenterResource extends BaseResource { @GET @Path("getFragments") @Produces("application/json") public Response getFragments(@Context final ServletContext servletContext, @Context final HttpHeaders headers) throws Throwable { LOG.debug("Received FRAGMENTER call"); startTime = System.currentTimeMillis(); final RequestContext context = parseRequest(headers); final String path = context.getDataSource(); final String fragmenterCacheKey = getFragmenterCacheKey(context); LOG.debug("FRAGMENTER started for path \"{}\"", path); List<Fragment> fragments; if (Utilities.isFragmenterCacheEnabled()) { try { fragments = fragmenterCacheFactory.getCache() .get(fragmenterCacheKey, new Callable<List<Fragment>>() { @Override public List<Fragment> call() throws Exception { didThreadProcessFragmentCall = true; LOG.debug("Caching fragments for transactionId={} from segmentId={} with key={}", context.getTransactionId(), context.getSegmentId(), fragmenterCacheKey); return getFragments(context); } }); } catch (UncheckedExecutionException | ExecutionException e) { if (e.getCause() != null) throw e.getCause(); throw e; } if (!didThreadProcessFragmentCall) { logFragmentStatistics(Level.DEBUG, context, fragments); } } else { LOG.debug("Fragmenter cache is disabled"); fragments = getFragments(context); } FragmentsResponse fragmentsResponse = FragmentsResponseFormatter.formatResponse(fragments, path); return Response.ok(fragmentsResponse, MediaType.APPLICATION_JSON_TYPE).build(); } FragmenterResource();   FragmenterResource(RequestParser<HttpHeaders> parser,
                       FragmenterFactory fragmenterFactory,
                       FragmenterCacheFactory fragmenterCacheFactory); }
FragmenterResource extends BaseResource { @GET @Path("getFragments") @Produces("application/json") public Response getFragments(@Context final ServletContext servletContext, @Context final HttpHeaders headers) throws Throwable { LOG.debug("Received FRAGMENTER call"); startTime = System.currentTimeMillis(); final RequestContext context = parseRequest(headers); final String path = context.getDataSource(); final String fragmenterCacheKey = getFragmenterCacheKey(context); LOG.debug("FRAGMENTER started for path \"{}\"", path); List<Fragment> fragments; if (Utilities.isFragmenterCacheEnabled()) { try { fragments = fragmenterCacheFactory.getCache() .get(fragmenterCacheKey, new Callable<List<Fragment>>() { @Override public List<Fragment> call() throws Exception { didThreadProcessFragmentCall = true; LOG.debug("Caching fragments for transactionId={} from segmentId={} with key={}", context.getTransactionId(), context.getSegmentId(), fragmenterCacheKey); return getFragments(context); } }); } catch (UncheckedExecutionException | ExecutionException e) { if (e.getCause() != null) throw e.getCause(); throw e; } if (!didThreadProcessFragmentCall) { logFragmentStatistics(Level.DEBUG, context, fragments); } } else { LOG.debug("Fragmenter cache is disabled"); fragments = getFragments(context); } FragmentsResponse fragmentsResponse = FragmentsResponseFormatter.formatResponse(fragments, path); return Response.ok(fragmentsResponse, MediaType.APPLICATION_JSON_TYPE).build(); } FragmenterResource();   FragmenterResource(RequestParser<HttpHeaders> parser,
                       FragmenterFactory fragmenterFactory,
                       FragmenterCacheFactory fragmenterCacheFactory); @GET @Path("getFragments") @Produces("application/json") Response getFragments(@Context final ServletContext servletContext,
                                 @Context final HttpHeaders headers); @GET @Path("getFragmentsStats") @Produces("application/json") Response getFragmentsStats(@Context final ServletContext servletContext,
                                      @Context final HttpHeaders headers,
                                      @QueryParam("path") final String path); }
FragmenterResource extends BaseResource { @GET @Path("getFragments") @Produces("application/json") public Response getFragments(@Context final ServletContext servletContext, @Context final HttpHeaders headers) throws Throwable { LOG.debug("Received FRAGMENTER call"); startTime = System.currentTimeMillis(); final RequestContext context = parseRequest(headers); final String path = context.getDataSource(); final String fragmenterCacheKey = getFragmenterCacheKey(context); LOG.debug("FRAGMENTER started for path \"{}\"", path); List<Fragment> fragments; if (Utilities.isFragmenterCacheEnabled()) { try { fragments = fragmenterCacheFactory.getCache() .get(fragmenterCacheKey, new Callable<List<Fragment>>() { @Override public List<Fragment> call() throws Exception { didThreadProcessFragmentCall = true; LOG.debug("Caching fragments for transactionId={} from segmentId={} with key={}", context.getTransactionId(), context.getSegmentId(), fragmenterCacheKey); return getFragments(context); } }); } catch (UncheckedExecutionException | ExecutionException e) { if (e.getCause() != null) throw e.getCause(); throw e; } if (!didThreadProcessFragmentCall) { logFragmentStatistics(Level.DEBUG, context, fragments); } } else { LOG.debug("Fragmenter cache is disabled"); fragments = getFragments(context); } FragmentsResponse fragmentsResponse = FragmentsResponseFormatter.formatResponse(fragments, path); return Response.ok(fragmentsResponse, MediaType.APPLICATION_JSON_TYPE).build(); } FragmenterResource();   FragmenterResource(RequestParser<HttpHeaders> parser,
                       FragmenterFactory fragmenterFactory,
                       FragmenterCacheFactory fragmenterCacheFactory); @GET @Path("getFragments") @Produces("application/json") Response getFragments(@Context final ServletContext servletContext,
                                 @Context final HttpHeaders headers); @GET @Path("getFragmentsStats") @Produces("application/json") Response getFragmentsStats(@Context final ServletContext servletContext,
                                      @Context final HttpHeaders headers,
                                      @QueryParam("path") final String path);  }