Vanlightly commented on a change in pull request #2936: URL: https://github.com/apache/bookkeeper/pull/2936#discussion_r772237897
########## File path: bookkeeper-server/src/main/java/org/apache/bookkeeper/bookie/datainteg/DataIntegrityCheckImpl.java ########## @@ -0,0 +1,554 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, + * software distributed under the License is distributed on an + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY + * KIND, either express or implied. See the License for the + * specific language governing permissions and limitations + * under the License. + */ + +package org.apache.bookkeeper.bookie.datainteg; + +import com.google.common.collect.ImmutableSortedMap; +import io.reactivex.rxjava3.core.Flowable; +import io.reactivex.rxjava3.core.Maybe; +import io.reactivex.rxjava3.core.Scheduler; +import io.reactivex.rxjava3.core.Single; +import java.io.IOException; +import java.util.Comparator; +import java.util.List; +import java.util.Map; +import java.util.NavigableMap; +import java.util.Optional; +import java.util.Set; +import java.util.UUID; +import java.util.concurrent.CompletableFuture; +import java.util.concurrent.ConcurrentSkipListMap; +import java.util.concurrent.TimeUnit; +import java.util.concurrent.atomic.AtomicInteger; +import java.util.concurrent.atomic.AtomicLong; +import java.util.concurrent.atomic.AtomicReference; +import java.util.stream.Collectors; + +import lombok.extern.slf4j.Slf4j; +import org.apache.bookkeeper.bookie.BookieException; +import org.apache.bookkeeper.bookie.LedgerStorage; +import org.apache.bookkeeper.bookie.LedgerStorage.StorageState; +import org.apache.bookkeeper.client.BKException; +import org.apache.bookkeeper.client.BookKeeperAdmin; +import org.apache.bookkeeper.client.api.LedgerMetadata; +import org.apache.bookkeeper.common.concurrent.FutureUtils; +import org.apache.bookkeeper.meta.LedgerManager; +import org.apache.bookkeeper.net.BookieId; + +/** + * An implementation of the DataIntegrityCheck interface. + */ +@Slf4j +public class DataIntegrityCheckImpl implements DataIntegrityCheck { + private static final int MAX_INFLIGHT = 300; + private static final int MAX_ENTRIES_INFLIGHT = 3000; + private static final int ZK_TIMEOUT_S = 30; + private final BookieId bookieId; + private final LedgerManager ledgerManager; + private final LedgerStorage ledgerStorage; + private final EntryCopier entryCopier; + private final BookKeeperAdmin admin; + private final Scheduler scheduler; + private final AtomicReference<Map<Long, LedgerMetadata>> ledgersCacheRef = + new AtomicReference<>(null); + private CompletableFuture<Void> preBootFuture; + + public DataIntegrityCheckImpl(BookieId bookieId, + LedgerManager ledgerManager, + LedgerStorage ledgerStorage, + EntryCopier entryCopier, + BookKeeperAdmin admin, + Scheduler scheduler) { + this.bookieId = bookieId; + this.ledgerManager = ledgerManager; + this.ledgerStorage = ledgerStorage; + this.entryCopier = entryCopier; + this.admin = admin; + this.scheduler = scheduler; + } + + @Override + public CompletableFuture<Void> runPreBoot(String reason) { + // we only run this once, it could be kicked off by different checks + synchronized (this) { + if (preBootFuture == null) { + preBootFuture = runPreBootSequence(reason); + } + } + return preBootFuture; + + } + + private CompletableFuture<Void> runPreBootSequence(String reason) { + String runId = UUID.randomUUID().toString(); + log.info("Event: {}, RunId: {}, Reason: {}", Events.PREBOOT_START, runId, reason); + try { + this.ledgerStorage.setStorageStateFlag(StorageState.NEEDS_INTEGRITY_CHECK); + } catch (IOException ioe) { + log.error("Event: {}, RunId: {}", Events.PREBOOT_ERROR, runId, ioe); + return FutureUtils.exception(ioe); + } + + MetadataAsyncIterator iter = new MetadataAsyncIterator(scheduler, + ledgerManager, MAX_INFLIGHT, ZK_TIMEOUT_S, TimeUnit.SECONDS); + CompletableFuture<Void> promise = new CompletableFuture<>(); + Map<Long, LedgerMetadata> ledgersCache = + new ConcurrentSkipListMap<>(Comparator.<Long>naturalOrder().reversed()); + iter.forEach((ledgerId, metadata) -> { + if (ensemblesContainBookie(metadata, bookieId)) { + ledgersCache.put(ledgerId, metadata); + try { + if (!ledgerStorage.ledgerExists(ledgerId)) { + ledgerStorage.setMasterKey(ledgerId, new byte[0]); + } + } catch (IOException ioe) { + return FutureUtils.exception(ioe); Review comment: Agreed. I've logged the error here with some more context. -- This is an automated message from the Apache Git Service. To respond to the message, please log on to GitHub and use the URL above to go to the specific comment. To unsubscribe, e-mail: [email protected] For queries about this service, please contact Infrastructure at: [email protected]
