/*
 * 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.flink.runtime.jobmaster;

import org.apache.flink.configuration.Configuration;
import org.apache.flink.configuration.CoreOptions;
import org.apache.flink.runtime.blob.BlobServer;
import org.apache.flink.runtime.blob.BlobWriter;
import org.apache.flink.runtime.execution.librarycache.BlobLibraryCacheManager;
import org.apache.flink.runtime.execution.librarycache.FlinkUserCodeClassLoaders;
import org.apache.flink.runtime.execution.librarycache.LibraryCacheManager;
import org.apache.flink.runtime.rpc.FatalErrorHandler;
import org.apache.flink.runtime.shuffle.ShuffleMaster;
import org.apache.flink.runtime.shuffle.ShuffleMasterContext;
import org.apache.flink.runtime.shuffle.ShuffleMasterContextImpl;
import org.apache.flink.runtime.shuffle.ShuffleServiceLoader;
import org.apache.flink.runtime.util.Hardware;
import org.apache.flink.util.ExceptionUtils;
import org.apache.flink.util.concurrent.ExecutorThreadFactory;

import javax.annotation.Nonnull;

import java.util.concurrent.Executors;
import java.util.concurrent.ScheduledExecutorService;

import static org.apache.flink.util.Preconditions.checkNotNull;

/**
 * Utility class which holds all auxiliary shared services used by the {@link JobMaster}.
 * Consequently, the {@link JobMaster} should never shut these services down.
 */
public class JobManagerSharedServices {

    private final ScheduledExecutorService scheduledExecutorService;

    private final LibraryCacheManager libraryCacheManager;

    private final ShuffleMaster<?> shuffleMaster;

    @Nonnull private final BlobWriter blobWriter;

    public JobManagerSharedServices(
            ScheduledExecutorService scheduledExecutorService,
            LibraryCacheManager libraryCacheManager,
            ShuffleMaster<?> shuffleMaster,
            @Nonnull BlobWriter blobWriter) {

        this.scheduledExecutorService = checkNotNull(scheduledExecutorService);
        this.libraryCacheManager = checkNotNull(libraryCacheManager);
        this.shuffleMaster = checkNotNull(shuffleMaster);
        this.blobWriter = blobWriter;
    }

    public ScheduledExecutorService getScheduledExecutorService() {
        return scheduledExecutorService;
    }

    public LibraryCacheManager getLibraryCacheManager() {
        return libraryCacheManager;
    }

    public ShuffleMaster<?> getShuffleMaster() {
        return shuffleMaster;
    }

    @Nonnull
    public BlobWriter getBlobWriter() {
        return blobWriter;
    }

    /**
     * Shutdown the {@link JobMaster} services.
     *
     * <p>This method makes sure all services are closed or shut down, even when an exception
     * occurred in the shutdown of one component. The first encountered exception is thrown, with
     * successive exceptions added as suppressed exceptions.
     *
     * @throws Exception The first Exception encountered during shutdown.
     */
    public void shutdown() throws Exception {
        Throwable firstException = null;

        try {
            scheduledExecutorService.shutdownNow();
        } catch (Throwable t) {
            firstException = t;
        }

        try {
            shuffleMaster.close();
        } catch (Throwable t) {
            firstException = firstException == null ? t : firstException;
        }

        libraryCacheManager.shutdown();

        if (firstException != null) {
            ExceptionUtils.rethrowException(
                    firstException, "Error while shutting down JobManager services");
        }
    }

    // ------------------------------------------------------------------------
    //  Creating the components from a configuration
    // ------------------------------------------------------------------------

    public static JobManagerSharedServices fromConfiguration(
            Configuration config, BlobServer blobServer, FatalErrorHandler fatalErrorHandler)
            throws Exception {

        checkNotNull(config);
        checkNotNull(blobServer);

        final String classLoaderResolveOrder =
                config.getString(CoreOptions.CLASSLOADER_RESOLVE_ORDER);

        final String[] alwaysParentFirstLoaderPatterns =
                CoreOptions.getParentFirstLoaderPatterns(config);

        final boolean failOnJvmMetaspaceOomError =
                config.getBoolean(CoreOptions.FAIL_ON_USER_CLASS_LOADING_METASPACE_OOM);
        final boolean checkClassLoaderLeak =
                config.getBoolean(CoreOptions.CHECK_LEAKED_CLASSLOADER);
        final BlobLibraryCacheManager libraryCacheManager =
                new BlobLibraryCacheManager(
                        blobServer,
                        BlobLibraryCacheManager.defaultClassLoaderFactory(
                                FlinkUserCodeClassLoaders.ResolveOrder.fromString(
                                        classLoaderResolveOrder),
                                alwaysParentFirstLoaderPatterns,
                                failOnJvmMetaspaceOomError ? fatalErrorHandler : null,
                                checkClassLoaderLeak));

        final ScheduledExecutorService futureExecutor =
                Executors.newScheduledThreadPool(
                        Hardware.getNumberCPUCores(),
                        new ExecutorThreadFactory("jobmanager-future"));

        final ShuffleMasterContext shuffleMasterContext =
                new ShuffleMasterContextImpl(config, fatalErrorHandler);
        final ShuffleMaster<?> shuffleMaster =
                ShuffleServiceLoader.loadShuffleServiceFactory(config)
                        .createShuffleMaster(shuffleMasterContext);
        shuffleMaster.start();

        return new JobManagerSharedServices(
                futureExecutor, libraryCacheManager, shuffleMaster, blobServer);
    }
}

@@ -3739,22 +3739,17 @@
 rowable 
-firstE
+e
 xception
@@ -3860,38 +3860,33 @@
 ) %7B%0A            
-firstE
+e
 xception = t;%0A  
@@ -3979,38 +3979,33 @@
 ) %7B%0A            
-firstE
+e
 xception = first
@@ -3999,21 +3999,16 @@
 ption = 
-first
 Exceptio
@@ -4012,37 +4012,44 @@
 tion
- == null ? t : firstE
+Utils.firstOrSuppressed(t, e
 xception
 ;%0A  
@@ -4044,16 +4044,17 @@
 xception
+)
 ;%0A      
@@ -4111,22 +4111,17 @@
     if (
-firstE
+e
 xception
@@ -4201,14 +4201,9 @@
     
-firstE
+e
 xcep

Use ExceptionUtils.firstOrSuppressed in JobManagerSharedServices