22
22
import org .elasticsearch .ElasticSearchException ;
23
23
import org .elasticsearch .ElasticSearchIllegalArgumentException ;
24
24
import org .elasticsearch .cloud .blobstore .CloudBlobStoreService ;
25
- import org .elasticsearch .cloud .jclouds .JCloudsUtils ;
26
25
import org .elasticsearch .cluster .ClusterName ;
27
26
import org .elasticsearch .cluster .metadata .MetaData ;
28
27
import org .elasticsearch .gateway .Gateway ;
48
47
import java .io .IOException ;
49
48
import java .util .Set ;
50
49
50
+ import static org .jclouds .blobstore .options .ListContainerOptions .Builder .*;
51
+
51
52
/**
52
53
* @author kimchy (shay.banon)
53
54
*/
@@ -60,9 +61,9 @@ public class CloudGateway extends AbstractLifecycleComponent<Gateway> implements
60
61
61
62
private final Location location ;
62
63
63
- private final SizeValue chunkSize ;
64
+ private final String metaDataDirectory ;
64
65
65
- private final String metadataContainer ;
66
+ private final SizeValue chunkSize ;
66
67
67
68
private volatile int currentIndex ;
68
69
@@ -90,17 +91,13 @@ public class CloudGateway extends AbstractLifecycleComponent<Gateway> implements
90
91
}
91
92
}
92
93
93
- String container = componentSettings .get ("container" );
94
+ this . container = componentSettings .get ("container" );
94
95
if (container == null ) {
95
96
throw new ElasticSearchIllegalArgumentException ("Cloud gateway requires 'container' setting" );
96
97
}
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 );
104
101
105
102
this .currentIndex = findLatestIndex ();
106
103
logger .debug ("Latest metadata found at index [" + currentIndex + "]" );
@@ -129,7 +126,7 @@ public SizeValue chunkSize() {
129
126
130
127
@ Override public void write (MetaData metaData ) throws GatewayException {
131
128
try {
132
- String name = " metadata-" + (currentIndex + 1 );
129
+ String name = metaDataDirectory + "/ metadata-" + (currentIndex + 1 );
133
130
134
131
BinaryXContentBuilder builder = XContentFactory .contentBinaryBuilder (XContentType .JSON );
135
132
builder .prettyPrint ();
@@ -141,14 +138,14 @@ public SizeValue chunkSize() {
141
138
blob .setPayload (new FastByteArrayInputStream (builder .unsafeBytes (), 0 , builder .unsafeBytesLength ()));
142
139
blob .setContentLength (builder .unsafeBytesLength ());
143
140
144
- blobStoreContext .getBlobStore ().putBlob (metadataContainer , blob );
141
+ blobStoreContext .getBlobStore ().putBlob (container , blob );
145
142
146
143
currentIndex ++;
147
144
148
- PageSet <? extends StorageMetadata > pageSet = blobStoreContext .getBlobStore ().list (metadataContainer );
145
+ PageSet <? extends StorageMetadata > pageSet = blobStoreContext .getBlobStore ().list (container , inDirectory ( metaDataDirectory ) );
149
146
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 ());
152
149
}
153
150
}
154
151
} catch (IOException e ) {
@@ -161,7 +158,7 @@ public SizeValue chunkSize() {
161
158
if (currentIndex == -1 )
162
159
return null ;
163
160
164
- return readMetaData (" metadata-" + currentIndex );
161
+ return readMetaData (metaDataDirectory + "/ metadata-" + currentIndex );
165
162
} catch (GatewayException e ) {
166
163
throw e ;
167
164
} catch (Exception e ) {
@@ -174,26 +171,26 @@ public SizeValue chunkSize() {
174
171
}
175
172
176
173
@ Override public void reset () {
177
- PageSet <? extends StorageMetadata > pageSet = blobStoreContext .getBlobStore ().list (metadataContainer );
174
+ PageSet <? extends StorageMetadata > pageSet = blobStoreContext .getBlobStore ().list (container , inDirectory ( metaDataDirectory ) );
178
175
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 ());
181
178
}
182
179
}
183
180
currentIndex = -1 ;
184
181
}
185
182
186
183
private int findLatestIndex () {
187
184
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 ) );
189
186
for (StorageMetadata storageMetadata : pageSet ) {
190
187
if (logger .isTraceEnabled ()) {
191
188
logger .trace ("[findLatestMetadata]: Processing blob [" + storageMetadata .getName () + "]" );
192
189
}
193
- if (!storageMetadata .getName ().startsWith ("metadata-" )) {
190
+ if (!storageMetadata .getName ().contains ("metadata-" )) {
194
191
continue ;
195
192
}
196
- int fileIndex = Integer .parseInt (storageMetadata .getName ().substring (storageMetadata .getName ().indexOf ('-' ) + 1 ));
193
+ int fileIndex = Integer .parseInt (storageMetadata .getName ().substring (storageMetadata .getName ().lastIndexOf ('-' ) + 1 ));
197
194
if (fileIndex >= index ) {
198
195
// try and read the meta data
199
196
try {
@@ -210,7 +207,7 @@ private int findLatestIndex() {
210
207
private MetaData readMetaData (String name ) throws IOException {
211
208
XContentParser parser = null ;
212
209
try {
213
- Blob blob = blobStoreContext .getBlobStore ().getBlob (metadataContainer , name );
210
+ Blob blob = blobStoreContext .getBlobStore ().getBlob (container , name );
214
211
parser = XContentFactory .xContent (XContentType .JSON ).createParser (blob .getContent ());
215
212
return MetaData .Builder .fromXContent (parser , settings );
216
213
} finally {
0 commit comments