Search in sources :

Example 1 with DeleteCallback

use of org.apache.bookkeeper.client.AsyncCallback.DeleteCallback in project pulsar by yahoo.

the class ManagedCursorImpl method createNewMetadataLedger.

void createNewMetadataLedger(final VoidCallback callback) {
    ledger.mbean.startCursorLedgerCreateOp();
    bookkeeper.asyncCreateLedger(config.getMetadataEnsemblesize(), config.getMetadataWriteQuorumSize(), config.getMetadataAckQuorumSize(), config.getDigestType(), config.getPassword(), (rc, lh, ctx) -> {
        ledger.getExecutor().submit(safeRun(() -> {
            ledger.mbean.endCursorLedgerCreateOp();
            if (rc != BKException.Code.OK) {
                log.warn("[{}] Error creating ledger for cursor {}: {}", ledger.getName(), name, BKException.getMessage(rc));
                callback.operationFailed(new ManagedLedgerException(BKException.getMessage(rc)));
                return;
            }
            if (log.isDebugEnabled()) {
                log.debug("[{}] Created ledger {} for cursor {}", ledger.getName(), lh.getId(), name);
            }
            // Created the ledger, now write the last position
            // content
            final PositionImpl position = (PositionImpl) getMarkDeletedPosition();
            persistPosition(lh, position, new VoidCallback() {

                @Override
                public void operationComplete() {
                    if (log.isDebugEnabled()) {
                        log.debug("[{}] Persisted position {} for cursor {}", ledger.getName(), position, name);
                    }
                    switchToNewLedger(lh, callback);
                }

                @Override
                public void operationFailed(ManagedLedgerException exception) {
                    log.warn("[{}] Failed to persist position {} for cursor {}", ledger.getName(), position, name);
                    ledger.mbean.startCursorLedgerDeleteOp();
                    bookkeeper.asyncDeleteLedger(lh.getId(), new DeleteCallback() {

                        @Override
                        public void deleteComplete(int rc, Object ctx) {
                            ledger.mbean.endCursorLedgerDeleteOp();
                        }
                    }, null);
                    callback.operationFailed(exception);
                }
            });
        }));
    }, null);
}
Also used : ManagedLedgerException(org.apache.bookkeeper.mledger.ManagedLedgerException) DeleteCallback(org.apache.bookkeeper.client.AsyncCallback.DeleteCallback) MarkDeleteCallback(org.apache.bookkeeper.mledger.AsyncCallbacks.MarkDeleteCallback)

Example 2 with DeleteCallback

use of org.apache.bookkeeper.client.AsyncCallback.DeleteCallback in project incubator-pulsar by apache.

the class ManagedCursorImpl method createNewMetadataLedger.

void createNewMetadataLedger(final VoidCallback callback) {
    ledger.mbean.startCursorLedgerCreateOp();
    bookkeeper.asyncCreateLedger(config.getMetadataEnsemblesize(), config.getMetadataWriteQuorumSize(), config.getMetadataAckQuorumSize(), config.getDigestType(), config.getPassword(), (rc, lh, ctx) -> {
        ledger.getExecutor().submit(safeRun(() -> {
            ledger.mbean.endCursorLedgerCreateOp();
            if (rc != BKException.Code.OK) {
                log.warn("[{}] Error creating ledger for cursor {}: {}", ledger.getName(), name, BKException.getMessage(rc));
                callback.operationFailed(new ManagedLedgerException(BKException.getMessage(rc)));
                return;
            }
            if (log.isDebugEnabled()) {
                log.debug("[{}] Created ledger {} for cursor {}", ledger.getName(), lh.getId(), name);
            }
            // Created the ledger, now write the last position
            // content
            MarkDeleteEntry mdEntry = lastMarkDeleteEntry;
            persistPositionToLedger(lh, mdEntry, new VoidCallback() {

                @Override
                public void operationComplete() {
                    if (log.isDebugEnabled()) {
                        log.debug("[{}] Persisted position {} for cursor {}", ledger.getName(), mdEntry.newPosition, name);
                    }
                    switchToNewLedger(lh, new VoidCallback() {

                        @Override
                        public void operationComplete() {
                            callback.operationComplete();
                        }

                        @Override
                        public void operationFailed(ManagedLedgerException exception) {
                            // it means it failed to switch the newly created ledger so, it should be
                            // deleted to prevent leak
                            bookkeeper.asyncDeleteLedger(lh.getId(), (int rc, Object ctx) -> {
                                if (rc != BKException.Code.OK) {
                                    log.warn("[{}] Failed to delete orphan ledger {}", ledger.getName(), lh.getId());
                                }
                            }, null);
                            callback.operationFailed(exception);
                        }
                    });
                }

                @Override
                public void operationFailed(ManagedLedgerException exception) {
                    log.warn("[{}] Failed to persist position {} for cursor {}", ledger.getName(), mdEntry.newPosition, name);
                    ledger.mbean.startCursorLedgerDeleteOp();
                    bookkeeper.asyncDeleteLedger(lh.getId(), new DeleteCallback() {

                        @Override
                        public void deleteComplete(int rc, Object ctx) {
                            ledger.mbean.endCursorLedgerDeleteOp();
                        }
                    }, null);
                    callback.operationFailed(exception);
                }
            });
        }));
    }, null, Collections.emptyMap());
}
Also used : ManagedLedgerException(org.apache.bookkeeper.mledger.ManagedLedgerException) ManagedLedgerException.getManagedLedgerException(org.apache.bookkeeper.mledger.ManagedLedgerException.getManagedLedgerException) ManagedLedgerImpl.createManagedLedgerException(org.apache.bookkeeper.mledger.impl.ManagedLedgerImpl.createManagedLedgerException) DeleteCallback(org.apache.bookkeeper.client.AsyncCallback.DeleteCallback) MarkDeleteCallback(org.apache.bookkeeper.mledger.AsyncCallbacks.MarkDeleteCallback)

Aggregations

DeleteCallback (org.apache.bookkeeper.client.AsyncCallback.DeleteCallback)2 MarkDeleteCallback (org.apache.bookkeeper.mledger.AsyncCallbacks.MarkDeleteCallback)2 ManagedLedgerException (org.apache.bookkeeper.mledger.ManagedLedgerException)2 ManagedLedgerException.getManagedLedgerException (org.apache.bookkeeper.mledger.ManagedLedgerException.getManagedLedgerException)1 ManagedLedgerImpl.createManagedLedgerException (org.apache.bookkeeper.mledger.impl.ManagedLedgerImpl.createManagedLedgerException)1