Java Code Examples for org.elasticsearch.index.translog.Translog#Location

The following examples show how to use org.elasticsearch.index.translog.Translog#Location . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: LocalTranslog.java    From Elasticsearch with Apache License 2.0 6 votes vote down vote up
public Location writeToLocal(BytesReference data) throws IOException {
    final long position;
    final long generation;
    try (ReleasableLock lock = writeLock.acquire()) {
        ensureOpen();
        if (writtenOffset > TRANSLOG_ROLLING_SIZE_BYTES) {
            IOUtils.close(writeChannel);
            tmpTranslogGeneration.incrementAndGet();
            writeChannel = FileChannel.open(this.translogPath.resolve(getFileNameFromId(tmpTranslogGeneration.get())), StandardOpenOption.CREATE_NEW, StandardOpenOption.WRITE);
            writtenOffset = 0;
        }
        generation = tmpTranslogGeneration.get();
        position = writtenOffset;
        try {
            data.writeTo(writeChannel);
        } catch (Throwable e) {
            throw e;
        }
        writtenOffset = writtenOffset + data.length();
    }
    return new Translog.Location(generation, position, data.length());
}
 
Example 2
Source File: IndexShard.java    From crate with Apache License 2.0 6 votes vote down vote up
/**
 * Add a listener for refreshes.
 *
 * @param location the location to listen for
 * @param listener for the refresh. Called with true if registering the listener ran it out of slots and forced a refresh. Called with
 *        false otherwise.
 */
public void addRefreshListener(Translog.Location location, Consumer<Boolean> listener) {
    final boolean readAllowed;
    if (isReadAllowed()) {
        readAllowed = true;
    } else {
        // check again under mutex. this is important to create a happens before relationship
        // between the switch to POST_RECOVERY + associated refresh. Otherwise we may respond
        // to a listener before a refresh actually happened that contained that operation.
        synchronized (mutex) {
            readAllowed = isReadAllowed();
        }
    }
    if (readAllowed) {
        refreshListeners.addOrNotify(location, listener);
    } else {
        // we're not yet ready fo ready for reads, just ignore refresh cycles
        listener.accept(false);
    }
}
 
Example 3
Source File: InternalEngine.java    From crate with Apache License 2.0 5 votes vote down vote up
@Override
public boolean ensureTranslogSynced(Stream<Translog.Location> locations) throws IOException {
    final boolean synced = translog.ensureSynced(locations);
    if (synced) {
        revisitIndexDeletionPolicyOnTranslogSynced();
    }
    return synced;
}
 
Example 4
Source File: RefreshListeners.java    From crate with Apache License 2.0 5 votes vote down vote up
/**
 * The number of pending listeners.
 */
public int pendingCount() {
    // No need to synchronize here because we're doing a single volatile read
    List<Tuple<Translog.Location, Consumer<Boolean>>> listeners = refreshListeners;
    // A null list means we haven't accumulated any listeners. Otherwise we need the size.
    return listeners == null ? 0 : listeners.size();
}
 
Example 5
Source File: TransportWriteAction.java    From crate with Apache License 2.0 5 votes vote down vote up
AsyncAfterWriteAction(final IndexShard indexShard,
                     @Nullable final Translog.Location location,
                     final RespondingWriteResult respond) {
    this.indexShard = indexShard;
    this.respond = respond;
    this.location = location;
    if ((sync = indexShard.getTranslogDurability() == Translog.Durability.REQUEST && location != null)) {
        pendingOps.incrementAndGet();
    }
    assert pendingOps.get() >= 0 && pendingOps.get() <= 3 : "pendingOpts was: " + pendingOps.get();
}
 
Example 6
Source File: TransportResyncReplicationAction.java    From crate with Apache License 2.0 4 votes vote down vote up
@Override
protected WriteReplicaResult shardOperationOnReplica(ResyncReplicationRequest request, IndexShard replica) throws Exception {
    Translog.Location location = performOnReplica(request, replica);
    return new WriteReplicaResult(request, location, null, replica, logger);
}
 
Example 7
Source File: IndexVersionValue.java    From crate with Apache License 2.0 4 votes vote down vote up
IndexVersionValue(Translog.Location translogLocation, long version, long seqNo, long term) {
    super(version, seqNo, term);
    this.translogLocation = translogLocation;
}
 
Example 8
Source File: InternalEngine.java    From crate with Apache License 2.0 4 votes vote down vote up
@Override
public Translog.Location getTranslogLastWriteLocation() {
    return getTranslog().getLastWriteLocation();
}
 
Example 9
Source File: InternalEngine.java    From Elasticsearch with Apache License 2.0 4 votes vote down vote up
private void innerDelete(Delete delete) throws IOException {
    synchronized (dirtyLock(delete.uid())) {
        final long currentVersion;
        VersionValue versionValue = versionMap.getUnderLock(delete.uid().bytes());
        if (versionValue == null) {
            currentVersion = loadCurrentVersionFromIndex(delete.uid());
        } else {
            if (engineConfig.isEnableGcDeletes() && versionValue.delete() && (engineConfig.getThreadPool().estimatedTimeInMillis() -
                    versionValue.time()) > engineConfig.getGcDeletesInMillis()) {
                currentVersion = Versions.NOT_FOUND; // deleted, and GC
            } else {
                currentVersion = versionValue.version();
            }
        }

        long updatedVersion;
        long expectedVersion = delete.version();
        if (delete.versionType().isVersionConflictForWrites(currentVersion, expectedVersion)) {
            if (delete.origin() == Operation.Origin.RECOVERY) {
                return;
            } else {
                throw new VersionConflictEngineException(shardId, delete.type(), delete.id(), currentVersion, expectedVersion);
            }
        }
        updatedVersion = delete.versionType().updateVersion(currentVersion, expectedVersion);
        final boolean found;
        if (currentVersion == Versions.NOT_FOUND) {
            // doc does not exist and no prior deletes
            found = false;
        } else if (versionValue != null && versionValue.delete()) {
            // a "delete on delete", in this case, we still increment the version, log it, and return that version
            found = false;
        } else {
            // we deleted a currently existing document
            indexWriter.deleteDocuments(delete.uid());
            found = true;
        }

        delete.updateVersion(updatedVersion, found);
        Translog.Location translogLocation = translog.add(new Translog.Delete(delete));
        versionMap.putUnderLock(delete.uid().bytes(), new DeleteVersionValue(updatedVersion, engineConfig.getThreadPool()
                .estimatedTimeInMillis(), translogLocation));
        delete.setTranslogLocation(translogLocation);
        indexingService.postDeleteUnderLock(delete);
    }
}
 
Example 10
Source File: InternalEngine.java    From Elasticsearch with Apache License 2.0 4 votes vote down vote up
private boolean innerIndex(Index index) throws IOException {
    synchronized (dirtyLock(index.uid())) {
        final long currentVersion;
        VersionValue versionValue = versionMap.getUnderLock(index.uid().bytes());
        if (versionValue == null) {
            currentVersion = loadCurrentVersionFromIndex(index.uid());
        } else {
            if (engineConfig.isEnableGcDeletes() && versionValue.delete() && (engineConfig.getThreadPool().estimatedTimeInMillis() -
                    versionValue.time()) > engineConfig.getGcDeletesInMillis()) {
                currentVersion = Versions.NOT_FOUND; // deleted, and GC
            } else {
                currentVersion = versionValue.version();
            }
        }

        long updatedVersion;
        long expectedVersion = index.version();
        if (index.versionType().isVersionConflictForWrites(currentVersion, expectedVersion)) {
            if (index.origin() == Operation.Origin.RECOVERY) {
                return false;
            } else {
                throw new VersionConflictEngineException(shardId, index.type(), index.id(), currentVersion, expectedVersion);
            }
        }
        // for reindex current version has to equal to expected version or it means the doc is updated
        if (index.reindex() && currentVersion != expectedVersion) {
            VersionConflictEngineException exception = new VersionConflictEngineException(shardId, index.type(), index.id(), currentVersion, expectedVersion);
            logger.info("reindex meet version conflict, maybe user deleted or updated the doc", exception);
            throw exception;
        }
        updatedVersion = index.versionType().updateVersion(currentVersion, expectedVersion);
        final boolean created;
        index.updateVersion(updatedVersion);
        if (currentVersion == Versions.NOT_FOUND) {
            // document does not exists, we can optimize for create
            created = true;
            if (index.docs().size() > 1) {
                indexWriter.addDocuments(index.docs());
            } else {
                indexWriter.addDocument(index.docs().get(0));
            }
        } else {
            if (versionValue != null) {
                created = versionValue.delete(); // we have a delete which is not GC'ed...
            } else {
                created = false;
            }
            if (index.docs().size() > 1) {
                indexWriter.updateDocuments(index.uid(), index.docs());
            } else {
                indexWriter.updateDocument(index.uid(), index.docs().get(0));
            }
        }
        Translog.Location translogLocation = translog.add(new Translog.Index(index));
        versionMap.putUnderLock(index.uid().bytes(), new VersionValue(updatedVersion, translogLocation));
        index.setTranslogLocation(translogLocation);
        indexingService.postIndexUnderLock(index);
        return created;
    }
}
 
Example 11
Source File: Engine.java    From Elasticsearch with Apache License 2.0 4 votes vote down vote up
public Translog.Location getTranslogLocation() {
    return this.location;
}
 
Example 12
Source File: Engine.java    From Elasticsearch with Apache License 2.0 4 votes vote down vote up
public void setTranslogLocation(Translog.Location location) {
    this.location = location;
}
 
Example 13
Source File: Engine.java    From Elasticsearch with Apache License 2.0 4 votes vote down vote up
public void setTranslogLocation(Translog.Location location) {
    this.location = location;
}
 
Example 14
Source File: VersionValue.java    From Elasticsearch with Apache License 2.0 4 votes vote down vote up
public Translog.Location translogLocation() {
    return this.translogLocation;
}
 
Example 15
Source File: DeleteVersionValue.java    From Elasticsearch with Apache License 2.0 4 votes vote down vote up
public DeleteVersionValue(long version, long time, Translog.Location translogLocation) {
    super(version, translogLocation);
    this.time = time;
}
 
Example 16
Source File: IndexVersionValue.java    From crate with Apache License 2.0 4 votes vote down vote up
@Override
public Translog.Location getLocation() {
    return translogLocation;
}
 
Example 17
Source File: TransportShardUpsertAction.java    From Elasticsearch with Apache License 2.0 4 votes vote down vote up
@Override
protected ShardResponse processRequestItems(ShardId shardId,
                                            ShardUpsertRequest request,
                                            AtomicBoolean killed) throws InterruptedException {
    ShardResponse shardResponse = new ShardResponse();
    DocTableInfo tableInfo = schemas.getWritableTable(TableIdent.fromIndexName(request.index()));
    IndexService indexService = indicesService.indexServiceSafe(shardId.getIndex());
    IndexShard indexShard = indexService.shardSafe(shardId.id());

    Translog.Location translogLocation = null;
    for (int i = 0; i < request.itemIndices().size(); i++) {
        int location = request.itemIndices().get(i);
        ShardUpsertRequest.Item item = request.items().get(i);
        if (killed.get()) {
            // set failure on response and skip all next items.
            // this way replica operation will be executed, but only items with a valid source (= was processed on primary)
            // will be processed on the replica
            shardResponse.failure(new InterruptedException());
            break;
        }
        try {
            translogLocation = indexItem(
                    tableInfo,
                    request,
                    item,
                    indexShard,
                    item.insertValues() != null, // try insert first
                    0);
            shardResponse.add(location);
        } catch (Throwable t) {
            if (retryPrimaryException(t)) {
                Throwables.propagate(t);
            }
            logger.debug("{} failed to execute upsert for [{}]/[{}]",
                    t, request.shardId(), request.type(), item.id());
            if (!request.continueOnError()) {
               shardResponse.failure(t);
               break;
            }
            shardResponse.add(location,
                    new ShardResponse.Failure(
                            item.id(),
                            ExceptionsHelper.detailedMessage(t),
                            (t instanceof VersionConflictEngineException)));
        }
    }
    if (indexShard.getTranslogDurability() == Translog.Durabilty.REQUEST && translogLocation != null) {
        indexShard.sync(translogLocation);
    }
    return shardResponse;
}
 
Example 18
Source File: test.java    From vscode-extension with MIT License 4 votes vote down vote up
@Override
public Translog.Location getTranslogLastWriteLocation() {
    return getTranslog().getLastWriteLocation();
}
 
Example 19
Source File: Engine.java    From crate with Apache License 2.0 2 votes vote down vote up
/**
 * Returns the last location that the translog of this engine has written into.
 */
public abstract Translog.Location getTranslogLastWriteLocation();
 
Example 20
Source File: DLBasedIndexShard.java    From Elasticsearch with Apache License 2.0 2 votes vote down vote up
/**
 * it is called by upper actions to ensure data is persisted into translog
 * currently it is useless
 */
public void sync(Translog.Location location) {
    return;
}