athanatos commented on a change in pull request #1819: ISSUE-1770: Add local checker for Sorted/InterleavedLedgerStorage URL: https://github.com/apache/bookkeeper/pull/1819#discussion_r239955768
########## File path: bookkeeper-server/src/main/java/org/apache/bookkeeper/server/service/ScrubberService.java ########## @@ -0,0 +1,141 @@ +/** + * + * 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.server.service; + + +import static org.apache.bookkeeper.bookie.ScrubberStats.DETECTED_FATAL_SCRUB_ERRORS; +import static org.apache.bookkeeper.bookie.ScrubberStats.DETECTED_SCRUB_ERRORS; +import static org.apache.bookkeeper.bookie.ScrubberStats.RUN_DURATION; + +import com.google.common.util.concurrent.RateLimiter; +import io.netty.util.concurrent.DefaultThreadFactory; +import java.io.IOException; +import java.util.List; +import java.util.Optional; +import java.util.Random; +import java.util.concurrent.Executors; +import java.util.concurrent.ScheduledExecutorService; +import java.util.concurrent.TimeUnit; +import java.util.concurrent.atomic.AtomicBoolean; +import org.apache.bookkeeper.bookie.ExitCode; +import org.apache.bookkeeper.bookie.LedgerStorage; +import org.apache.bookkeeper.server.component.ServerLifecycleComponent; +import org.apache.bookkeeper.server.conf.BookieConfiguration; +import org.apache.bookkeeper.stats.Counter; +import org.apache.bookkeeper.stats.OpStatsLogger; +import org.apache.bookkeeper.stats.StatsLogger; +import org.apache.bookkeeper.util.MathUtils; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; + +/** + * A {@link org.apache.bookkeeper.common.component.LifecycleComponent} that runs stats provider. + */ +public class ScrubberService extends ServerLifecycleComponent { + private static final Logger LOG = LoggerFactory.getLogger(ScrubberService.class); + + private static final String NAME = "scrubber"; + private final ScheduledExecutorService executor; + private final Random rng = new Random(); + private final long scrubPeriod; + private final Optional<RateLimiter> scrubRateLimiter; + private final AtomicBoolean stop = new AtomicBoolean(false); + private final LedgerStorage ledgerStorage; + + private final OpStatsLogger scrubCounter; + private final Counter errorCounter; + private final Counter fatalErrorCounter; + + public ScrubberService( + StatsLogger logger, + BookieConfiguration conf, + LedgerStorage ledgerStorage) { + super(NAME, conf, logger); + this.executor = Executors.newSingleThreadScheduledExecutor( + new DefaultThreadFactory("ScrubThread")); + this.scrubPeriod = conf.getServerConf().getLocalScrubPeriod(); + + double rateLimit = conf.getServerConf().getLocalScrubRateLimit(); + this.scrubRateLimiter = rateLimit == 0 ? Optional.empty() : Optional.of(RateLimiter.create(rateLimit)); + + this.ledgerStorage = ledgerStorage; + + this.scrubCounter = logger.getOpStatsLogger(RUN_DURATION); + this.errorCounter = logger.getCounter(DETECTED_SCRUB_ERRORS); + this.fatalErrorCounter = logger.getCounter(DETECTED_FATAL_SCRUB_ERRORS); + } + + private long getNextPeriodMS() { + return (long) (((double) scrubPeriod) * (1.5 - rng.nextDouble()) * 1000); Review comment: The service won't be instantiated otherwise. ---------------------------------------------------------------- This is an automated message from the Apache Git Service. To respond to the message, please log on GitHub and use the URL above to go to the specific comment. For queries about this service, please contact Infrastructure at: [email protected] With regards, Apache Git Services
