content: /*
   * 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.beam.runners.flink.translation.utils;
  
  import com.fasterxml.jackson.databind.type.TypeFactory;
  import java.lang.reflect.Field;
  import org.apache.beam.runners.core.TimerInternals;
  import org.apache.flink.runtime.state.InternalPriorityQueue;
  import org.apache.flink.streaming.api.operators.InternalTimer;
  import org.apache.flink.streaming.api.operators.InternalTimerService;
  import org.apache.flink.streaming.api.operators.InternalTimerServiceImpl;
  
  /** Workarounds for dealing with limitations of Flink or its libraries. */
  public class Workarounds {
  
    public static void deleteStaticCaches() {
      // Clear cache to get rid of any references to the Flink Classloader
      // See https://jira.apache.org/jira/browse/BEAM-6460
      TypeFactory.defaultInstance().clearCache();
    }
  
    @SuppressWarnings("all")
    public static InternalPriorityQueue<InternalTimer<Object, TimerInternals.TimerData>>
        retrieveInternalProcessingTimerQueue(
            InternalTimerService<TimerInternals.TimerData> timerService) {
      Field internalProcessingTimerQueue = null;
      try {
        internalProcessingTimerQueue =
            InternalTimerServiceImpl.class.getDeclaredField("processingTimeTimersQueue");
        internalProcessingTimerQueue.setAccessible(true);
        return (InternalPriorityQueue) internalProcessingTimerQueue.get(timerService);
      } catch (Exception e) {
        throw new IllegalStateException("Failed to retrieve processing timer queue.", e);
      } finally {
        if (internalProcessingTimerQueue != null) {
          internalProcessingTimerQueue.setAccessible(false);
        }
      }
    }
  }
  