示例#1
0
  @Test
  public void testRefCount() throws IOException {
    final ShardId shardId = new ShardId(new Index("index"), 1);
    DirectoryService directoryService = new LuceneManagedDirectoryService(random());
    Store store =
        new Store(
            shardId,
            ImmutableSettings.EMPTY,
            directoryService,
            randomDistributor(directoryService),
            new DummyShardLock(shardId));
    int incs = randomIntBetween(1, 100);
    for (int i = 0; i < incs; i++) {
      if (randomBoolean()) {
        store.incRef();
      } else {
        assertTrue(store.tryIncRef());
      }
      store.ensureOpen();
    }

    for (int i = 0; i < incs; i++) {
      store.decRef();
      store.ensureOpen();
    }

    store.incRef();
    final AtomicBoolean called = new AtomicBoolean(false);
    store.close();
    for (int i = 0; i < incs; i++) {
      if (randomBoolean()) {
        store.incRef();
      } else {
        assertTrue(store.tryIncRef());
      }
      store.ensureOpen();
    }

    for (int i = 0; i < incs; i++) {
      store.decRef();
      store.ensureOpen();
    }

    store.decRef();
    assertThat(store.refCount(), Matchers.equalTo(0));
    assertFalse(store.tryIncRef());
    try {
      store.incRef();
      fail(" expected exception");
    } catch (AlreadyClosedException ex) {

    }
    try {
      store.ensureOpen();
      fail(" expected exception");
    } catch (AlreadyClosedException ex) {

    }
  }
示例#2
0
 /** Deletes the content of a shard store. Be careful calling this!. */
 public void deleteContent() throws IOException {
   ensureOpen();
   String[] files = directory.listAll();
   IOException lastException = null;
   for (String file : files) {
     if (isChecksum(file)) {
       try {
         directory.deleteFileChecksum(file);
       } catch (IOException e) {
         lastException = e;
       }
     } else {
       try {
         directory.deleteFile(file);
       } catch (NoSuchFileException | FileNotFoundException e) {
         // ignore
       } catch (IOException e) {
         lastException = e;
       }
     }
   }
   if (lastException != null) {
     throw lastException;
   }
 }
示例#3
0
 public void writeChecksums() throws IOException {
   ensureOpen();
   ImmutableMap<String, StoreFileMetaData> files = list();
   String checksumName = CHECKSUMS_PREFIX + System.currentTimeMillis();
   synchronized (mutex) {
     Map<String, String> checksums = new HashMap<>();
     for (StoreFileMetaData metaData : files.values()) {
       if (metaData.checksum() != null) {
         checksums.put(metaData.name(), metaData.checksum());
       }
     }
     while (directory.fileExists(checksumName)) {
       checksumName = CHECKSUMS_PREFIX + System.currentTimeMillis();
     }
     try (IndexOutput output = directory.createOutput(checksumName, IOContext.DEFAULT, true)) {
       output.writeInt(0); // version
       output.writeStringStringMap(checksums);
     }
   }
   for (StoreFileMetaData metaData : files.values()) {
     if (metaData.name().startsWith(CHECKSUMS_PREFIX) && !checksumName.equals(metaData.name())) {
       try {
         directory.deleteFileChecksum(metaData.name());
       } catch (Throwable e) {
         // ignore
       }
     }
   }
 }
示例#4
0
 /** Opened an index input in raw form, no decompression for example. */
 public IndexInput openInputRaw(String name, IOContext context) throws IOException {
   ensureOpen();
   StoreFileMetaData metaData = filesMetadata.get(name);
   if (metaData == null) {
     throw new FileNotFoundException(name);
   }
   return metaData.directory().openInput(name, context);
 }
示例#5
0
 public void writeChecksum(String name, String checksum) throws IOException {
   ensureOpen();
   // update the metadata to include the checksum and write a new checksums file
   synchronized (mutex) {
     StoreFileMetaData metaData = filesMetadata.get(name);
     metaData =
         new StoreFileMetaData(metaData.name(), metaData.length(), checksum, metaData.directory());
     filesMetadata = ImmutableOpenMap.builder(filesMetadata).fPut(name, metaData).build();
     writeChecksums();
   }
 }
示例#6
0
 public ImmutableMap<String, StoreFileMetaData> list() throws IOException {
   ensureOpen();
   ImmutableMap.Builder<String, StoreFileMetaData> builder = ImmutableMap.builder();
   for (String name : files) {
     StoreFileMetaData md = metaData(name);
     if (md != null) {
       builder.put(md.name(), md);
     }
   }
   return builder.build();
 }
示例#7
0
 public StoreFileMetaData metaData(String name) throws IOException {
   ensureOpen();
   StoreFileMetaData md = filesMetadata.get(name);
   if (md == null) {
     return null;
   }
   // IndexOutput not closed, does not exists
   if (md.length() == -1) {
     return null;
   }
   return md;
 }
示例#8
0
 public void writeChecksums(Map<String, String> checksums) throws IOException {
   ensureOpen();
   // update the metadata to include the checksum and write a new checksums file
   synchronized (mutex) {
     for (Map.Entry<String, String> entry : checksums.entrySet()) {
       StoreFileMetaData metaData = filesMetadata.get(entry.getKey());
       metaData =
           new StoreFileMetaData(
               metaData.name(), metaData.length(), entry.getValue(), metaData.directory());
       filesMetadata =
           ImmutableOpenMap.builder(filesMetadata).fPut(entry.getKey(), metaData).build();
     }
     writeChecksums();
   }
 }
示例#9
0
 public void renameFile(String from, String to) throws IOException {
   ensureOpen();
   synchronized (mutex) {
     StoreFileMetaData fromMetaData = filesMetadata.get(from); // we should always find this one
     if (fromMetaData == null) {
       throw new FileNotFoundException(from);
     }
     directoryService.renameFile(fromMetaData.directory(), from, to);
     StoreFileMetaData toMetaData =
         new StoreFileMetaData(
             to, fromMetaData.length(), fromMetaData.checksum(), fromMetaData.directory());
     filesMetadata =
         ImmutableOpenMap.builder(filesMetadata).fRemove(from).fPut(to, toMetaData).build();
     files = filesMetadata.keys().toArray(String.class);
   }
 }
示例#10
0
 /** Creates a raw output, no checksum is computed, and no compression if enabled. */
 public IndexOutput createOutputRaw(String name) throws IOException {
   ensureOpen();
   return directory.createOutput(name, IOContext.DEFAULT, true);
 }
示例#11
0
 public ByteSizeValue estimateSize() throws IOException {
   ensureOpen();
   return new ByteSizeValue(Directories.estimateSize(directory));
 }
示例#12
0
 public StoreStats stats() throws IOException {
   ensureOpen();
   return new StoreStats(
       Directories.estimateSize(directory), directoryService.throttleTimeInNanos());
 }
示例#13
0
 public Directory directory() {
   ensureOpen();
   return directory;
 }
示例#14
0
 public IndexStore indexStore() {
   ensureOpen();
   return this.indexStore;
 }