/** {@inheritDoc} */
  @SuppressWarnings({"CatchGenericClass"})
  @Override
  public final void run() {
    try {
      body();
    } catch (InterruptedException e) {
      if (log.isDebugEnabled()) {
        log.debug("Caught interrupted exception: " + e);
      }
    }
    // Catch everything to make sure that it gets logged properly and
    // not to kill any threads from the underlying thread pool.
    catch (Throwable e) {
      U.error(log, "Runtime error caught during grid runnable execution: " + this, e);
    } finally {
      cleanup();

      if (log.isDebugEnabled()) {
        if (isInterrupted()) {
          log.debug(
              "Grid runnable finished due to interruption without cancellation: " + getName());
        } else {
          log.debug("Grid runnable finished normally: " + getName());
        }
      }
    }
  }
  /** {@inheritDoc} */
  @Override
  public void spiStop() throws GridSpiException {
    unregisterMBean();

    // Ack ok stop.
    if (log.isDebugEnabled()) log.debug(stopInfo());
  }
  /** {@inheritDoc} */
  @Nullable
  @Override
  public GridDeployment explicitDeploy(Class<?> cls, ClassLoader clsLdr) throws GridException {
    if (log.isDebugEnabled())
      log.debug("Ignoring explicit deploy [cls=" + cls + ", clsLdr=" + clsLdr + ']');

    return null;
  }
  /**
   * Gets job priority. At first tries to get from job context. If job context has no priority, then
   * tries to get from task session. If task session has no priority default one will be used.
   *
   * @param ctx Collision job context.
   * @return Job priority.
   */
  private int getJobPriority(GridCollisionJobContext ctx) {
    assert ctx != null;

    Integer p = null;

    GridJobContext jctx = ctx.getJobContext();

    try {
      p = (Integer) jctx.getAttribute(jobAttrKey);
    } catch (ClassCastException e) {
      LT.error(
          log,
          e,
          "Type of job context priority attribute '"
              + jobAttrKey
              + "' is not java.lang.Integer [type="
              + jctx.getAttribute(jobAttrKey).getClass()
              + ']');
    }

    if (p == null) {
      GridTaskSession ses = ctx.getTaskSession();

      try {
        p = (Integer) ses.getAttribute(taskAttrKey);
      } catch (ClassCastException e) {
        LT.error(
            log,
            e,
            "Type of task session priority attribute '"
                + taskAttrKey
                + "' is not java.lang.Integer [type="
                + ses.getAttribute(taskAttrKey).getClass()
                + ']');
      }

      if (p == null) {
        if (log.isDebugEnabled()) {
          log.debug(
              "Failed get priority from job context attribute '"
                  + jobAttrKey
                  + "' and task session attribute '"
                  + taskAttrKey
                  + "' (will use default priority): "
                  + dfltPriority);
        }

        p = dfltPriority;
      }
    }

    assert p != null;

    return p;
  }
  /** {@inheritDoc} */
  @Override
  public void spiStart(String gridName) throws GridSpiException {
    assertParameter(parallelJobsNum > 0, "parallelJobsNum > 0");
    assertParameter(waitJobsNum >= 0, "waitingJobsNum >= 0");
    assertParameter(taskAttrKey != null, "taskAttrKey != null");
    assertParameter(jobAttrKey != null, "jobAttrKey != null");

    // Start SPI start stopwatch.
    startStopwatch();

    // Ack parameters.
    if (log.isDebugEnabled()) {
      log.debug(configInfo("parallelJobsNum", parallelJobsNum));
      log.debug(configInfo("taskAttrKey", taskAttrKey));
      log.debug(configInfo("jobAttrKey", jobAttrKey));
      log.debug(configInfo("dfltPriority", dfltPriority));
      log.debug(configInfo("starvationInc", starvationInc));
      log.debug(configInfo("preventStarvation", preventStarvation));
    }

    registerMBean(gridName, this, GridPriorityQueueCollisionSpiMBean.class);

    // Ack start.
    if (log.isDebugEnabled()) log.debug(startInfo());
  }
  /** @throws GridException If operation failed. */
  private void initializeLatch() throws GridException {
    if (initGuard.compareAndSet(false, true)) {
      try {
        internalLatch =
            CU.outTx(
                new Callable<CountDownLatch>() {
                  @Override
                  public CountDownLatch call() throws Exception {
                    GridCacheTx tx =
                        CU.txStartInternal(ctx, latchView, PESSIMISTIC, REPEATABLE_READ);

                    try {
                      GridCacheCountDownLatchValue val = latchView.get(key);

                      if (val == null) {
                        if (log.isDebugEnabled())
                          log.debug("Failed to find count down latch with given name: " + name);

                        assert cnt == 0;

                        return new CountDownLatch(cnt);
                      }

                      tx.commit();

                      return new CountDownLatch(val.get());
                    } finally {
                      tx.end();
                    }
                  }
                },
                ctx);

        if (log.isDebugEnabled()) log.debug("Initialized internal latch: " + internalLatch);
      } finally {
        initLatch.countDown();
      }
    } else {
      try {
        initLatch.await();
      } catch (InterruptedException ignored) {
        throw new GridException("Thread has been interrupted.");
      }

      if (internalLatch == null)
        throw new GridException("Internal latch has not been properly initialized.");
    }
  }
  /**
   * @param gridName Grid instance name. Can be {@code null}.
   * @param exec Executor service.
   * @param parentLog Parent logger.
   */
  static void runBackgroundCheck(String gridName, Executor exec, GridLogger parentLog) {
    assert exec != null;
    assert parentLog != null;

    final GridLogger log = parentLog.getLogger(GridDiagnostic.class);

    try {
      exec.execute(
          new GridWorker(gridName, "grid-diagnostic-1", log) {
            @Override
            public void body() {
              try {
                InetAddress localHost = U.getLocalHost();

                if (!localHost.isReachable(REACH_TIMEOUT)) {
                  U.warn(
                      log,
                      "Default local host is unreachable. This may lead to delays on "
                          + "grid network operations. Check your OS network setting to correct it.",
                      "Default local host is unreachable.");
                }
              } catch (IOException ignore) {
                U.warn(
                    log,
                    "Failed to perform network diagnostics. It is usually caused by serious "
                        + "network configuration problem. Check your OS network setting to correct it.",
                    "Failed to perform network diagnostics.");
              }
            }
          });

      exec.execute(
          new GridWorker(gridName, "grid-diagnostic-2", log) {
            @Override
            public void body() {
              try {
                InetAddress localHost = U.getLocalHost();

                if (localHost.isLoopbackAddress()) {
                  U.warn(
                      log,
                      "Default local host is a loopback address. This can be a sign of "
                          + "potential network configuration problem.",
                      "Default local host is a loopback address.");
                }
              } catch (IOException ignore) {
                U.warn(
                    log,
                    "Failed to perform network diagnostics. It is usually caused by serious "
                        + "network configuration problem. Check your OS network setting to correct it.",
                    "Failed to perform network diagnostics.");
              }
            }
          });

      exec.execute(
          new GridWorker(gridName, "grid-diagnostic-3", log) {
            @Override
            public void body() {
              String jdkStrLow = U.jdkString().toLowerCase();

              if (jdkStrLow.contains("jrockit") && jdkStrLow.contains("1.5.")) {
                U.warn(
                    log,
                    "BEA JRockit VM ver. 1.5.x has shown problems with NIO functionality in our "
                        + "tests that were not reproducible in other VMs. We recommend using Sun VM. Should you "
                        + "have further questions please contact us at [email protected]",
                    "BEA JRockit VM ver. 1.5.x is not supported.");
              }
            }
          });

      exec.execute(
          new GridWorker(gridName, "grid-diagnostic-4", log) {
            @Override
            public void body() {
              // Sufficiently tested OS.
              if (!U.isSufficientlyTestedOs()) {
                U.warn(
                    log,
                    "This operating system has been tested less rigorously: "
                        + U.osString()
                        + ". Our team will appreciate the feedback if you experience any problems running "
                        + "gridgain in this environment. You can always send your feedback to [email protected]",
                    "This OS is tested less rigorously: " + U.osString());
              }
            }
          });

      exec.execute(
          new GridWorker(gridName, "grid-diagnostic-5", log) {
            @Override
            public void body() {
              // Fix for GG-1075.
              if (U.allLocalMACs() == null)
                U.warn(
                    log,
                    "No live network interfaces detected. If IP-multicast discovery is used - "
                        + "make sure to add 127.0.0.1 as a local address.",
                    "No live network interfaces. Add 127.0.0.1 as a local address.");
            }
          });

      exec.execute(
          new GridWorker(gridName, "grid-diagnostic-6", log) {
            @Override
            public void body() {
              if (System.getProperty("com.sun.management.jmxremote") != null) {
                String portStr = System.getProperty("com.sun.management.jmxremote.port");

                if (portStr != null)
                  try {
                    Integer.parseInt(portStr);

                    return;
                  } catch (NumberFormatException ignore) {
                  }

                U.warn(
                    log,
                    "JMX remote management is enabled but JMX port is either not set or invalid. "
                        + "Check system property 'com.sun.management.jmxremote.port' to make sure it specifies "
                        + "valid TCP/IP port.",
                    "JMX remote port is invalid - JMX management is off.");
              }
            }
          });
    } catch (RejectedExecutionException e) {
      U.error(
          log,
          "Failed to start background network diagnostics check due to thread pool execution "
              + "rejection. In most cases it indicates a severe configuration problem with GridGain.",
          "Failed to start background network diagnostics.",
          e);
    }
  }
 /** {@inheritDoc} */
 @Override
 public void onKernalStart() throws GridException {
   if (log.isDebugEnabled()) log.debug("Ignoring kernel started callback: " + this);
 }