Skip to content

Commit

Permalink
Releasable XContentBuilder
Browse files Browse the repository at this point in the history
make the builder releasable (auto closeable), and use it in shards state
also make XContentParser releasable (AutoCloseable) and not closeable since it doesn't throw an IOException
closes #6869
  • Loading branch information
kimchy committed Jul 15, 2014
1 parent 9345194 commit 3232107
Show file tree
Hide file tree
Showing 5 changed files with 19 additions and 20 deletions.
Expand Up @@ -25,7 +25,6 @@
import com.google.common.collect.Lists;
import com.google.common.collect.Maps;
import org.apache.lucene.util.CollectionUtil;
import org.apache.lucene.util.IOUtils;
import org.elasticsearch.ElasticsearchException;
import org.elasticsearch.Version;
import org.elasticsearch.action.ActionListener;
Expand All @@ -48,6 +47,7 @@
import org.elasticsearch.common.compress.CompressedString;
import org.elasticsearch.common.inject.Inject;
import org.elasticsearch.common.io.Streams;
import org.elasticsearch.common.lease.Releasables;
import org.elasticsearch.common.regex.Regex;
import org.elasticsearch.common.settings.ImmutableSettings;
import org.elasticsearch.common.settings.Settings;
Expand Down Expand Up @@ -491,7 +491,7 @@ private List<IndexTemplateMetaData> findTemplates(CreateIndexClusterStateUpdateR
} catch (Exception e) {
logger.warn("[{}] failed to read template [{}] from config", e, request.index(), templatesFile.getAbsolutePath());
} finally {
IOUtils.closeWhileHandlingException(parser);
Releasables.closeWhileHandlingException(parser);
}
}
}
Expand Down
Expand Up @@ -27,6 +27,7 @@
import org.elasticsearch.common.geo.GeoPoint;
import org.elasticsearch.common.io.BytesStream;
import org.elasticsearch.common.io.stream.BytesStreamOutput;
import org.elasticsearch.common.lease.Releasable;
import org.elasticsearch.common.text.Text;
import org.elasticsearch.common.unit.ByteSizeValue;
import org.elasticsearch.common.unit.TimeValue;
Expand All @@ -47,7 +48,7 @@
/**
*
*/
public final class XContentBuilder implements BytesStream {
public final class XContentBuilder implements BytesStream, Releasable {

public static enum FieldCaseConversion {
/**
Expand Down Expand Up @@ -1094,8 +1095,6 @@ public BytesStream bytesStream() throws IOException {

/**
* Returns a string representation of the builder (only applicable for text based xcontent).
* <p/>
* <p>Only applicable when the builder is constructed with {@link FastByteArrayOutputStream}.
*/
public String string() throws IOException {
close();
Expand Down
Expand Up @@ -20,6 +20,7 @@
package org.elasticsearch.common.xcontent;

import org.apache.lucene.util.BytesRef;
import org.elasticsearch.common.lease.Releasable;

import java.io.Closeable;
import java.io.IOException;
Expand All @@ -35,7 +36,7 @@
* XContentParser parser = xContentType.xContent().createParser("{\"key\" : \"value\"}");
* </pre>
*/
public interface XContentParser extends Closeable {
public interface XContentParser extends Releasable {

enum Token {
START_OBJECT {
Expand Down Expand Up @@ -196,6 +197,4 @@ enum NumberType {
boolean booleanValue() throws IOException;

byte[] binaryValue() throws IOException;

void close();
}
Expand Up @@ -267,15 +267,17 @@ private ShardStateInfo readShardState(byte[] data) throws Exception {

private void writeShardState(String reason, ShardId shardId, ShardStateInfo shardStateInfo, @Nullable ShardStateInfo previousStateInfo) throws Exception {
logger.trace("[{}][{}] writing shard state, reason [{}]", shardId.index().name(), shardId.id(), reason);
XContentBuilder builder = XContentFactory.contentBuilder(XContentType.JSON, new BytesStreamOutput());
builder.prettyPrint();
builder.startObject();
builder.field("version", shardStateInfo.version);
if (shardStateInfo.primary != null) {
builder.field("primary", shardStateInfo.primary);
BytesReference shardState;
try (XContentBuilder builder = XContentFactory.contentBuilder(XContentType.JSON, new BytesStreamOutput())) {
builder.prettyPrint();
builder.startObject();
builder.field("version", shardStateInfo.version);
if (shardStateInfo.primary != null) {
builder.field("primary", shardStateInfo.primary);
}
builder.endObject();
shardState = builder.bytes();
}
builder.endObject();
builder.flush();

Exception lastFailure = null;
boolean wroteAtLeastOnce = false;
Expand All @@ -288,8 +290,7 @@ private void writeShardState(String reason, ShardId shardId, ShardStateInfo shar
FileOutputStream fos = null;
try {
fos = new FileOutputStream(stateFile);
BytesReference bytes = builder.bytes();
bytes.writeTo(fos);
shardState.writeTo(fos);
fos.getChannel().force(true);
fos.close();
wroteAtLeastOnce = true;
Expand Down
4 changes: 2 additions & 2 deletions src/main/java/org/elasticsearch/search/SearchService.java
Expand Up @@ -26,7 +26,6 @@
import org.apache.lucene.index.AtomicReaderContext;
import org.apache.lucene.index.NumericDocValues;
import org.apache.lucene.search.TopDocs;
import org.apache.lucene.util.IOUtils;
import org.elasticsearch.ElasticsearchException;
import org.elasticsearch.ElasticsearchParseException;
import org.elasticsearch.ExceptionsHelper;
Expand All @@ -39,6 +38,7 @@
import org.elasticsearch.common.bytes.BytesReference;
import org.elasticsearch.common.component.AbstractLifecycleComponent;
import org.elasticsearch.common.inject.Inject;
import org.elasticsearch.common.lease.Releasables;
import org.elasticsearch.common.settings.Settings;
import org.elasticsearch.common.unit.TimeValue;
import org.elasticsearch.common.util.BigArrays;
Expand Down Expand Up @@ -611,7 +611,7 @@ private void parseTemplate(ShardSearchRequest request) {
} catch (IOException e) {
throw new ElasticsearchParseException("Failed to parse template", e);
} finally {
IOUtils.closeWhileHandlingException(parser);
Releasables.closeWhileHandlingException(parser);
}

if (templateContext == null || !hasLength(templateContext.template())) {
Expand Down

0 comments on commit 3232107

Please sign in to comment.