All Downloads are FREE. Search and download functionalities are using the official Maven repository.

com.pingcap.tispark.handler.CacheInvalidateEventHandler.scala Maven / Gradle / Ivy

/*
 *
 * Copyright 2017 PingCAP, Inc.
 *
 * Licensed 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 com.pingcap.tispark.handler

import org.tikv.common.event.CacheInvalidateEvent.CacheType
import com.pingcap.tispark.listener.CacheInvalidateListener
import org.slf4j.LoggerFactory
import org.tikv.common.event.CacheInvalidateEvent
import org.tikv.common.region.RegionManager

/**
 * A CacheInvalidateEventHandler as it's name indicates what this class will do.
 *
 * Since there's only one event and one event handler currently in the project, we
 * don't need to over-design our event handler to support millions of other events.
 *
 * Refactor this if we need to support tons of events.
 *
 * @param regionManager Region manager used for sending invalidating cache. Usually
 *                      it's Spark driver's regionManager
 */
class CacheInvalidateEventHandler(regionManager: RegionManager) {
  private final val logger = LoggerFactory.getLogger(getClass.getName)

  def handle(event: CacheInvalidateEvent): Unit = {
    try {
      event.getCacheType match {
        case CacheType.REGION_STORE =>
          // Used for updating region/store cache in the given regionManager
          if (event.shouldUpdateRegion()) {
            logger.info(s"Invalidating region ${event.getRegionId} cache at driver.")
            val region = regionManager.getRegionById(event.getRegionId)
            if (region != null) {
              regionManager.invalidateRegion(region)
            }
          }

          if (event.shouldUpdateStore()) {
            logger.info(s"Invalidating store ${event.getStoreId} cache at driver.")
            regionManager.invalidateStore(event.getStoreId)
          }
        case CacheType.LEADER =>
          // Used for updating leader information cached in the given regionManager
          logger.info(
            s"Invalidating leader of region:${event.getRegionId} store:${event.getStoreId} cache at driver.")
          val region = regionManager.getRegionById(event.getRegionId)
          if (region != null) {
            regionManager.updateLeader(region, event.getStoreId)
            regionManager.invalidateRegion(region)
          }

        case CacheType.REQ_FAILED =>
          logger.info(s"Request failed cache invalidation for region ${event.getRegionId}")
          val region = regionManager.getRegionById(event.getRegionId)
          if (region != null) {
            regionManager.onRequestFail(region)
          }
        case _ => throw new IllegalArgumentException("Unsupported cache invalidate type.")
      }
    } catch {
      case e: Exception =>
        logger.error(s"Updating cache failed:${e.getMessage}")
        return
    }
    CacheInvalidateListener.getInstance().CACHE_INVALIDATE_ACCUMULATOR.remove(event)
  }
}

object CacheInvalidateEventHandler {
  def apply(regionManager: RegionManager): CacheInvalidateEventHandler =
    new CacheInvalidateEventHandler(regionManager)
}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy