Skip to content

Commit c7075c1

Browse files
committed
Cloud Plugin: Gateway should store meta data and indices under the same container, closes #180.
1 parent 6185e43 commit c7075c1

File tree

6 files changed

+92
-80
lines changed

6 files changed

+92
-80
lines changed

.idea/dictionaries/kimchy.xml

Lines changed: 1 addition & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

.idea/modules/plugins-cloud.iml

Lines changed: 2 additions & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

plugins/cloud/src/main/java/org/elasticsearch/cloud/jclouds/JCloudsUtils.java

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -29,8 +29,6 @@
2929
*/
3030
public class JCloudsUtils {
3131

32-
public static final String BLOB_CONTAINER_SEP = "-";
33-
3432
public static Iterable<? extends Module> buildModules(Settings settings) {
3533
return ImmutableList.of(new JCloudsLoggingModule(settings));
3634
}

plugins/cloud/src/main/java/org/elasticsearch/gateway/cloud/CloudGateway.java

Lines changed: 21 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,6 @@
2222
import org.elasticsearch.ElasticSearchException;
2323
import org.elasticsearch.ElasticSearchIllegalArgumentException;
2424
import org.elasticsearch.cloud.blobstore.CloudBlobStoreService;
25-
import org.elasticsearch.cloud.jclouds.JCloudsUtils;
2625
import org.elasticsearch.cluster.ClusterName;
2726
import org.elasticsearch.cluster.metadata.MetaData;
2827
import org.elasticsearch.gateway.Gateway;
@@ -48,6 +47,8 @@
4847
import java.io.IOException;
4948
import java.util.Set;
5049

50+
import static org.jclouds.blobstore.options.ListContainerOptions.Builder.*;
51+
5152
/**
5253
* @author kimchy (shay.banon)
5354
*/
@@ -60,9 +61,9 @@ public class CloudGateway extends AbstractLifecycleComponent<Gateway> implements
6061

6162
private final Location location;
6263

63-
private final SizeValue chunkSize;
64+
private final String metaDataDirectory;
6465

65-
private final String metadataContainer;
66+
private final SizeValue chunkSize;
6667

6768
private volatile int currentIndex;
6869

@@ -90,17 +91,13 @@ public class CloudGateway extends AbstractLifecycleComponent<Gateway> implements
9091
}
9192
}
9293

93-
String container = componentSettings.get("container");
94+
this.container = componentSettings.get("container");
9495
if (container == null) {
9596
throw new ElasticSearchIllegalArgumentException("Cloud gateway requires 'container' setting");
9697
}
97-
this.container = container + JCloudsUtils.BLOB_CONTAINER_SEP + clusterName.value();
98-
99-
this.metadataContainer = this.container + JCloudsUtils.BLOB_CONTAINER_SEP + "metadata";
100-
101-
logger.debug("Using location [{}], container [{}], metadata_container [{}]", this.location, this.container, metadataContainer);
102-
103-
blobStoreContext.getBlobStore().createContainerInLocation(this.location, metadataContainer);
98+
this.metaDataDirectory = clusterName.value() + "/metadata";
99+
logger.debug("Using location [{}], container [{}], metadata_directory [{}]", this.location, this.container, metaDataDirectory);
100+
blobStoreContext.getBlobStore().createContainerInLocation(this.location, container);
104101

105102
this.currentIndex = findLatestIndex();
106103
logger.debug("Latest metadata found at index [" + currentIndex + "]");
@@ -129,7 +126,7 @@ public SizeValue chunkSize() {
129126

130127
@Override public void write(MetaData metaData) throws GatewayException {
131128
try {
132-
String name = "metadata-" + (currentIndex + 1);
129+
String name = metaDataDirectory + "/metadata-" + (currentIndex + 1);
133130

134131
BinaryXContentBuilder builder = XContentFactory.contentBinaryBuilder(XContentType.JSON);
135132
builder.prettyPrint();
@@ -141,14 +138,14 @@ public SizeValue chunkSize() {
141138
blob.setPayload(new FastByteArrayInputStream(builder.unsafeBytes(), 0, builder.unsafeBytesLength()));
142139
blob.setContentLength(builder.unsafeBytesLength());
143140

144-
blobStoreContext.getBlobStore().putBlob(metadataContainer, blob);
141+
blobStoreContext.getBlobStore().putBlob(container, blob);
145142

146143
currentIndex++;
147144

148-
PageSet<? extends StorageMetadata> pageSet = blobStoreContext.getBlobStore().list(metadataContainer);
145+
PageSet<? extends StorageMetadata> pageSet = blobStoreContext.getBlobStore().list(container, inDirectory(metaDataDirectory));
149146
for (StorageMetadata storageMetadata : pageSet) {
150-
if (storageMetadata.getName().startsWith("metadata-") && !name.equals(storageMetadata.getName())) {
151-
blobStoreContext.getAsyncBlobStore().removeBlob(metadataContainer, storageMetadata.getName());
147+
if (storageMetadata.getName().contains("metadata-") && !name.equals(storageMetadata.getName())) {
148+
blobStoreContext.getAsyncBlobStore().removeBlob(container, storageMetadata.getName());
152149
}
153150
}
154151
} catch (IOException e) {
@@ -161,7 +158,7 @@ public SizeValue chunkSize() {
161158
if (currentIndex == -1)
162159
return null;
163160

164-
return readMetaData("metadata-" + currentIndex);
161+
return readMetaData(metaDataDirectory + "/metadata-" + currentIndex);
165162
} catch (GatewayException e) {
166163
throw e;
167164
} catch (Exception e) {
@@ -174,26 +171,26 @@ public SizeValue chunkSize() {
174171
}
175172

176173
@Override public void reset() {
177-
PageSet<? extends StorageMetadata> pageSet = blobStoreContext.getBlobStore().list(metadataContainer);
174+
PageSet<? extends StorageMetadata> pageSet = blobStoreContext.getBlobStore().list(container, inDirectory(metaDataDirectory));
178175
for (StorageMetadata storageMetadata : pageSet) {
179-
if (storageMetadata.getName().startsWith("metadata-")) {
180-
blobStoreContext.getBlobStore().removeBlob(metadataContainer, storageMetadata.getName());
176+
if (storageMetadata.getName().contains("metadata-")) {
177+
blobStoreContext.getBlobStore().removeBlob(container, storageMetadata.getName());
181178
}
182179
}
183180
currentIndex = -1;
184181
}
185182

186183
private int findLatestIndex() {
187184
int index = -1;
188-
PageSet<? extends StorageMetadata> pageSet = blobStoreContext.getBlobStore().list(metadataContainer);
185+
PageSet<? extends StorageMetadata> pageSet = blobStoreContext.getBlobStore().list(container, inDirectory(metaDataDirectory).maxResults(1000));
189186
for (StorageMetadata storageMetadata : pageSet) {
190187
if (logger.isTraceEnabled()) {
191188
logger.trace("[findLatestMetadata]: Processing blob [" + storageMetadata.getName() + "]");
192189
}
193-
if (!storageMetadata.getName().startsWith("metadata-")) {
190+
if (!storageMetadata.getName().contains("metadata-")) {
194191
continue;
195192
}
196-
int fileIndex = Integer.parseInt(storageMetadata.getName().substring(storageMetadata.getName().indexOf('-') + 1));
193+
int fileIndex = Integer.parseInt(storageMetadata.getName().substring(storageMetadata.getName().lastIndexOf('-') + 1));
197194
if (fileIndex >= index) {
198195
// try and read the meta data
199196
try {
@@ -210,7 +207,7 @@ private int findLatestIndex() {
210207
private MetaData readMetaData(String name) throws IOException {
211208
XContentParser parser = null;
212209
try {
213-
Blob blob = blobStoreContext.getBlobStore().getBlob(metadataContainer, name);
210+
Blob blob = blobStoreContext.getBlobStore().getBlob(container, name);
214211
parser = XContentFactory.xContent(XContentType.JSON).createParser(blob.getContent());
215212
return MetaData.Builder.fromXContent(parser, settings);
216213
} finally {

plugins/cloud/src/main/java/org/elasticsearch/index/gateway/cloud/CloudIndexGateway.java

Lines changed: 11 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@
2222
import org.elasticsearch.ElasticSearchException;
2323
import org.elasticsearch.ElasticSearchIllegalArgumentException;
2424
import org.elasticsearch.cloud.blobstore.CloudBlobStoreService;
25-
import org.elasticsearch.cloud.jclouds.JCloudsUtils;
25+
import org.elasticsearch.cluster.ClusterName;
2626
import org.elasticsearch.gateway.Gateway;
2727
import org.elasticsearch.gateway.cloud.CloudGateway;
2828
import org.elasticsearch.index.AbstractIndexComponent;
@@ -48,13 +48,15 @@ public class CloudIndexGateway extends AbstractIndexComponent implements IndexGa
4848

4949
private final String indexContainer;
5050

51+
private final String indexDirectory;
52+
5153
private final Location location;
5254

5355
private final SizeValue chunkSize;
5456

5557
private final BlobStoreContext blobStoreContext;
5658

57-
@Inject public CloudIndexGateway(Index index, @IndexSettings Settings indexSettings, CloudBlobStoreService blobStoreService, Gateway gateway) {
59+
@Inject public CloudIndexGateway(Index index, @IndexSettings Settings indexSettings, ClusterName clusterName, CloudBlobStoreService blobStoreService, Gateway gateway) {
5860
super(index, indexSettings);
5961
this.blobStoreContext = blobStoreService.context();
6062
this.gateway = gateway;
@@ -66,7 +68,7 @@ public class CloudIndexGateway extends AbstractIndexComponent implements IndexGa
6668
if (gateway instanceof CloudGateway) {
6769
CloudGateway cloudGateway = (CloudGateway) gateway;
6870
if (container == null) {
69-
container = cloudGateway.container() + JCloudsUtils.BLOB_CONTAINER_SEP + index.name();
71+
container = cloudGateway.container();
7072
}
7173
if (chunkSize == null) {
7274
chunkSize = cloudGateway.chunkSize();
@@ -99,11 +101,10 @@ public class CloudIndexGateway extends AbstractIndexComponent implements IndexGa
99101
}
100102
}
101103
this.indexContainer = container;
104+
this.indexDirectory = clusterName.value() + "/" + index.name();
102105
this.chunkSize = chunkSize;
103106

104-
logger.debug("Using location [{}], container [{}], chunk_size [{}]", this.location, this.indexContainer, this.chunkSize);
105-
106-
// blobStoreContext.getBlobStore().createContainerInLocation(this.location, this.indexContainer);
107+
logger.debug("Using location [{}], container [{}], index_directory [{}], chunk_size [{}]", this.location, this.indexContainer, this.indexDirectory, this.chunkSize);
107108
}
108109

109110
public Location indexLocation() {
@@ -114,6 +115,10 @@ public String indexContainer() {
114115
return this.indexContainer;
115116
}
116117

118+
public String indexDirectory() {
119+
return this.indexDirectory;
120+
}
121+
117122
public SizeValue chunkSize() {
118123
return this.chunkSize;
119124
}
@@ -126,6 +131,5 @@ public SizeValue chunkSize() {
126131
if (!delete) {
127132
return;
128133
}
129-
// blobStoreContext.getBlobStore().deleteContainer(indexContainer);
130134
}
131135
}

0 commit comments

Comments
 (0)