List of usage examples for com.google.common.io CountingOutputStream close
@Override
public void close() throws IOException
From source file:io.druid.query.aggregation.atomcube.AtomCubeQueryResource.java
@POST
@Produces({ MediaType.APPLICATION_JSON, SmileMediaTypes.APPLICATION_JACKSON_SMILE })
@Consumes({ MediaType.APPLICATION_JSON, SmileMediaTypes.APPLICATION_JACKSON_SMILE, APPLICATION_SMILE })
public Response doPost(InputStream in, @QueryParam("pretty") String pretty,
@Context final HttpServletRequest req // used only to get request content-type and remote address
) throws IOException {
final long start = System.currentTimeMillis();
final String reqContentType = req.getContentType();
final boolean isSmile = SmileMediaTypes.APPLICATION_JACKSON_SMILE.equals(reqContentType)
|| APPLICATION_SMILE.equals(reqContentType);
ObjectMapper objectMapper = isSmile ? smileMapper : jsonMapper;
final String contentType = isSmile ? SmileMediaTypes.APPLICATION_JACKSON_SMILE : MediaType.APPLICATION_JSON;
final ObjectWriter jsonWriter = pretty != null ? objectMapper.writerWithDefaultPrettyPrinter()
: objectMapper.writer();// w ww .j a v a2s . c om
AtomCubeQuery _query = objectMapper.readValue(in, AtomCubeQuery.class);
final AtomCubeQuery atomQ = (AtomCubeQuery) _query.withId(UUID.randomUUID().toString());
final Map<String, Object> responseContext1 = new MapMaker().makeMap();
Sequence res = atomQ.run(this.conglomerate.findFactory(atomQ).createRunner(null), responseContext1);
final Sequence results;
if (res == null) {
results = Sequences.empty();
} else {
results = res;
}
final Yielder yielder = results.toYielder(null, new YieldingAccumulator() {
@Override
public Object accumulate(Object accumulated, Object in) {
yield();
return in;
}
});
final Map<String, Object> responseContext = new MapMaker().makeMap();
Response.ResponseBuilder builder = Response.ok(new StreamingOutput() {
@Override
public void write(OutputStream outputStream) throws IOException, WebApplicationException {
CountingOutputStream os = new CountingOutputStream(outputStream);
jsonWriter.writeValue(os, yielder);
os.flush();
os.close();
final long queryTime = System.currentTimeMillis() - start;
emitter.emit(DruidMetrics.makeQueryTimeMetric(null, jsonMapper, atomQ, req.getRemoteAddr())
.setDimension("success", "true").build("query/time", queryTime));
emitter.emit(DruidMetrics.makeQueryTimeMetric(null, jsonMapper, atomQ, req.getRemoteAddr())
.build("query/bytes", os.getCount()));
requestLogger.log(new RequestLogLine(new DateTime(), req.getRemoteAddr(), atomQ,
new QueryStats(ImmutableMap.<String, Object>of("query/time", queryTime, "query/bytes",
os.getCount(), "success", true))));
}
}, contentType).header("X-Druid-Query-Id", atomQ.getId());
String responseCtxString = jsonMapper.writeValueAsString(responseContext);
return builder.header("X-Druid-Response-Context", responseCtxString).build();
}
From source file:org.gradle.internal.resource.local.LocalFileStandInExternalResource.java
@Override public ExternalResourceWriteResult put(ReadableContent location) { try {// w w w . j a v a 2 s . c o m if (!localFile.canWrite()) { localFile.delete(); } Files.createParentDirs(localFile); InputStream input = location.open(); try { CountingOutputStream output = new CountingOutputStream(new FileOutputStream(localFile)); try { IOUtils.copyLarge(input, output); } finally { output.close(); } return new ExternalResourceWriteResult(output.getCount()); } finally { input.close(); } } catch (IOException e) { throw ResourceExceptions.putFailed(getURI(), e); } }
From source file:org.glowroot.agent.fat.storage.util.CappedDatabase.java
private long write(String type, Copier copier) throws IOException { synchronized (lock) { if (closing) { return -1; }/*from w ww . j av a 2s. c o m*/ long startTick = ticker.read(); out.startBlock(); NonClosingCountingOutputStream countingStreamAfterCompression = new NonClosingCountingOutputStream(out); CountingOutputStream countingStreamBeforeCompression = new CountingOutputStream( new LZFOutputStream(countingStreamAfterCompression)); copier.copyTo(countingStreamBeforeCompression); countingStreamBeforeCompression.close(); long endTick = ticker.read(); CappedDatabaseStats stats = statsByType.get(type); if (stats == null) { stats = new CappedDatabaseStats(); statsByType.put(type, stats); } stats.record(countingStreamBeforeCompression.getCount(), countingStreamAfterCompression.getCount(), endTick - startTick); return out.endBlock(); } }
From source file:org.glowroot.agent.embedded.util.CappedDatabase.java
private long write(String type, Copier copier) throws IOException { long blockStartIndex; synchronized (lock) { if (closed) { return -1; }/*from w w w .j a v a 2 s. c o m*/ long startTick = ticker.read(); out.startBlock(); NonClosingCountingOutputStream countingStreamAfterCompression = new NonClosingCountingOutputStream(out); CountingOutputStream countingStreamBeforeCompression = new CountingOutputStream( newLZFOutputStream(countingStreamAfterCompression)); copier.copyTo(countingStreamBeforeCompression); countingStreamBeforeCompression.close(); long endTick = ticker.read(); CappedDatabaseStats stats = statsByType.get(type); if (stats == null) { stats = new CappedDatabaseStats(); statsByType.put(type, stats); } stats.record(countingStreamBeforeCompression.getCount(), countingStreamAfterCompression.getCount(), endTick - startTick); blockStartIndex = out.endBlock(); } // fsync (if really needed here) does not need to be done under lock out.fsyncIfReallyNeeded(); return blockStartIndex; }
From source file:org.apache.druid.server.QueryResource.java
@POST
@Produces({ MediaType.APPLICATION_JSON, SmileMediaTypes.APPLICATION_JACKSON_SMILE })
@Consumes({ MediaType.APPLICATION_JSON, SmileMediaTypes.APPLICATION_JACKSON_SMILE, APPLICATION_SMILE })
public Response doPost(final InputStream in, @QueryParam("pretty") final String pretty,
@Context final HttpServletRequest req // used to get request content-type,Accept header, remote address and auth-related headers
) throws IOException {
final QueryLifecycle queryLifecycle = queryLifecycleFactory.factorize();
Query<?> query = null;/*from w w w .j a v a 2 s. c o m*/
String acceptHeader = req.getHeader("Accept");
if (Strings.isNullOrEmpty(acceptHeader)) {
//default to content-type
acceptHeader = req.getContentType();
}
final ResponseContext context = createContext(acceptHeader, pretty != null);
final String currThreadName = Thread.currentThread().getName();
try {
queryLifecycle.initialize(readQuery(req, in, context));
query = queryLifecycle.getQuery();
final String queryId = query.getId();
Thread.currentThread().setName(StringUtils.format("%s[%s_%s_%s]", currThreadName, query.getType(),
query.getDataSource().getNames(), queryId));
if (log.isDebugEnabled()) {
log.debug("Got query [%s]", query);
}
final Access authResult = queryLifecycle.authorize(req);
if (!authResult.isAllowed()) {
throw new ForbiddenException(authResult.toString());
}
final QueryLifecycle.QueryResponse queryResponse = queryLifecycle.execute();
final Sequence<?> results = queryResponse.getResults();
final Map<String, Object> responseContext = queryResponse.getResponseContext();
final String prevEtag = getPreviousEtag(req);
if (prevEtag != null && prevEtag.equals(responseContext.get(HEADER_ETAG))) {
return Response.notModified().build();
}
final Yielder<?> yielder = Yielders.each(results);
try {
boolean shouldFinalize = QueryContexts.isFinalize(query, true);
boolean serializeDateTimeAsLong = QueryContexts.isSerializeDateTimeAsLong(query, false)
|| (!shouldFinalize && QueryContexts.isSerializeDateTimeAsLongInner(query, false));
final ObjectWriter jsonWriter = context.newOutputWriter(serializeDateTimeAsLong);
Response.ResponseBuilder builder = Response.ok(new StreamingOutput() {
@Override
public void write(OutputStream outputStream) throws WebApplicationException {
Exception e = null;
CountingOutputStream os = new CountingOutputStream(outputStream);
try {
// json serializer will always close the yielder
jsonWriter.writeValue(os, yielder);
os.flush(); // Some types of OutputStream suppress flush errors in the .close() method.
os.close();
} catch (Exception ex) {
e = ex;
log.error(ex, "Unable to send query response.");
throw Throwables.propagate(ex);
} finally {
Thread.currentThread().setName(currThreadName);
queryLifecycle.emitLogsAndMetrics(e, req.getRemoteAddr(), os.getCount());
if (e == null) {
successfulQueryCount.incrementAndGet();
} else {
failedQueryCount.incrementAndGet();
}
}
}
}, context.getContentType()).header("X-Druid-Query-Id", queryId);
if (responseContext.get(HEADER_ETAG) != null) {
builder.header(HEADER_ETAG, responseContext.get(HEADER_ETAG));
responseContext.remove(HEADER_ETAG);
}
DirectDruidClient.removeMagicResponseContextFields(responseContext);
//Limit the response-context header, see https://github.com/apache/incubator-druid/issues/2331
//Note that Response.ResponseBuilder.header(String key,Object value).build() calls value.toString()
//and encodes the string using ASCII, so 1 char is = 1 byte
String responseCtxString = jsonMapper.writeValueAsString(responseContext);
if (responseCtxString.length() > RESPONSE_CTX_HEADER_LEN_LIMIT) {
log.warn("Response Context truncated for id [%s] . Full context is [%s].", queryId,
responseCtxString);
responseCtxString = responseCtxString.substring(0, RESPONSE_CTX_HEADER_LEN_LIMIT);
}
return builder.header("X-Druid-Response-Context", responseCtxString).build();
} catch (Exception e) {
// make sure to close yielder if anything happened before starting to serialize the response.
yielder.close();
throw Throwables.propagate(e);
} finally {
// do not close yielder here, since we do not want to close the yielder prior to
// StreamingOutput having iterated over all the results
}
} catch (QueryInterruptedException e) {
interruptedQueryCount.incrementAndGet();
queryLifecycle.emitLogsAndMetrics(e, req.getRemoteAddr(), -1);
return context.gotError(e);
} catch (ForbiddenException e) {
// don't do anything for an authorization failure, ForbiddenExceptionMapper will catch this later and
// send an error response if this is thrown.
throw e;
} catch (Exception e) {
failedQueryCount.incrementAndGet();
queryLifecycle.emitLogsAndMetrics(e, req.getRemoteAddr(), -1);
log.makeAlert(e, "Exception handling request").addData("exception", e.toString())
.addData("query", query != null ? query.toString() : "unparseable query")
.addData("peer", req.getRemoteAddr()).emit();
return context.gotError(e);
} finally {
Thread.currentThread().setName(currThreadName);
}
}