private void _loadIndexFromCluster(IndexAccessor indexAccessor, long localLastGeneration) {

    List<Address> clusterNodeAddresses = ClusterExecutorUtil.getClusterNodeAddresses();

    int clusterNodeAddressesCount = clusterNodeAddresses.size();

    if (clusterNodeAddressesCount <= 1) {
      if (_log.isDebugEnabled()) {
        _log.debug(
            "Do not load indexes because there is either one portal "
                + "instance or no portal instances in the cluster");
      }

      return;
    }

    ClusterRequest clusterRequest =
        ClusterRequest.createMulticastRequest(
            new MethodHandler(_getLastGenerationMethodKey, indexAccessor.getCompanyId()), true);

    ClusterExecutorUtil.execute(
        clusterRequest,
        new LoadIndexClusterResponseCallback(
            indexAccessor, clusterNodeAddressesCount, localLastGeneration));
  }
    @Override
    public void processClusterEvent(ClusterEvent clusterEvent) {
      ClusterEventType clusterEventType = clusterEvent.getClusterEventType();

      if (!clusterEventType.equals(ClusterEventType.JOIN)) {
        return;
      }

      List<Address> clusterNodeAddresses = ClusterExecutorUtil.getClusterNodeAddresses();
      List<ClusterNode> clusterNodes = clusterEvent.getClusterNodes();

      if ((clusterNodeAddresses.size() - clusterNodes.size()) > 1) {
        if (_log.isDebugEnabled()) {
          _log.debug("Number of original cluster members is greater than " + "one");
        }

        return;
      }

      long[] companyIds = PortalInstances.getCompanyIds();

      for (long companyId : companyIds) {
        loadIndexes(companyId);
      }

      loadIndexes(CompanyConstants.SYSTEM);
    }
Exemple #3
0
  @Test
  public void testSlaveToMaster() throws Exception {
    _clusterSchedulerEngine = _getClusterSchedulerEngine(false, 2, 0);

    Assert.assertFalse(_isMaster(_clusterSchedulerEngine));

    Map<String, SchedulerResponse> schedulerResponseMap =
        _getMemoryClusteredJobs(_clusterSchedulerEngine);

    Assert.assertEquals(2, schedulerResponseMap.size());

    String newMaster =
        AddressSerializerUtil.serialize(ClusterExecutorUtil.getLocalClusterNodeAddress());

    MockLockLocalService.setLock(newMaster);

    Assert.assertTrue(_isMaster(_clusterSchedulerEngine));

    _clusterSchedulerEngine.pause(_TEST_JOB_NAME_0, _MEMORY_CLUSTER_TEST_GROUP_NAME);

    _clusterSchedulerEngine.getScheduledJobs();

    schedulerResponseMap = _getMemoryClusteredJobs(_clusterSchedulerEngine);

    Assert.assertTrue(schedulerResponseMap.isEmpty());
  }
  private static void _registerClusterOrder(
      HttpServletRequest request,
      ClusterNode clusterNode,
      String orderUuid,
      String productEntryName,
      int maxServers)
      throws Exception {

    MethodHandler methodHandler =
        new MethodHandler(_registerOrderMethodKey, orderUuid, productEntryName, maxServers);

    ClusterRequest clusterRequest =
        ClusterRequest.createUnicastRequest(methodHandler, clusterNode.getClusterNodeId());

    FutureClusterResponses futureClusterResponses = ClusterExecutorUtil.execute(clusterRequest);

    ClusterNodeResponses clusterNodeResponses =
        futureClusterResponses.get(20000, TimeUnit.MILLISECONDS);

    ClusterNodeResponse clusterNodeResponse = clusterNodeResponses.getClusterResponse(clusterNode);

    Map<String, Object> attributes = (Map<String, Object>) clusterNodeResponse.getResult();

    for (Map.Entry<String, Object> entry : attributes.entrySet()) {
      request.setAttribute(
          clusterNode.getClusterNodeId() + StringPool.UNDERLINE + entry.getKey(), entry.getValue());
    }
  }
  private void verifyClusterGroup() throws ManageActionException {
    List<ClusterNode> clusterNodes = ClusterExecutorUtil.getClusterNodes();

    String[] requiredClusterNodesIds = _clusterGroup.getClusterNodeIdsArray();

    for (String requiredClusterNodeId : requiredClusterNodesIds) {
      boolean verified = false;

      Iterator<ClusterNode> itr = clusterNodes.iterator();

      while (itr.hasNext()) {
        ClusterNode clusterNode = itr.next();

        String clusterNodeId = clusterNode.getClusterNodeId();

        if (clusterNodeId.equals(requiredClusterNodeId)) {
          itr.remove();

          verified = true;

          break;
        }
      }

      if (!verified) {
        throw new ManageActionException(
            "Cluster node " + requiredClusterNodeId + " is not available");
      }
    }
  }
Exemple #6
0
  @After
  public void tearDown() throws Exception {
    if (_clusterSchedulerEngine != null) {
      _clusterSchedulerEngine.shutdown();
    }

    ClusterExecutorUtil.destroy();
  }
  @Override
  public void shutdown() {
    if (_luceneIndexThreadPoolExecutor != null) {
      _luceneIndexThreadPoolExecutor.shutdownNow();

      try {
        _luceneIndexThreadPoolExecutor.awaitTermination(60, TimeUnit.SECONDS);
      } catch (InterruptedException ie) {
        _log.error("Lucene indexer shutdown interrupted", ie);
      }
    }

    if (isLoadIndexFromClusterEnabled()) {
      ClusterExecutorUtil.removeClusterEventListener(_loadIndexClusterEventListener);
    }

    MessageBus messageBus = MessageBusUtil.getMessageBus();

    for (String searchEngineId : SearchEngineUtil.getSearchEngineIds()) {
      String searchWriterDestinationName =
          SearchEngineUtil.getSearchWriterDestinationName(searchEngineId);

      Destination searchWriteDestination = messageBus.getDestination(searchWriterDestinationName);

      if (searchWriteDestination != null) {
        ThreadPoolExecutor threadPoolExecutor =
            PortalExecutorManagerUtil.getPortalExecutor(searchWriterDestinationName);

        int maxPoolSize = threadPoolExecutor.getMaxPoolSize();

        CountDownLatch countDownLatch = new CountDownLatch(maxPoolSize);

        ShutdownSyncJob shutdownSyncJob = new ShutdownSyncJob(countDownLatch);

        for (int i = 0; i < maxPoolSize; i++) {
          threadPoolExecutor.submit(shutdownSyncJob);
        }

        try {
          countDownLatch.await();
        } catch (InterruptedException ie) {
          _log.error("Shutdown waiting interrupted", ie);
        }

        List<Runnable> runnables = threadPoolExecutor.shutdownNow();

        if (_log.isDebugEnabled()) {
          _log.debug("Cancelled appending indexing jobs: " + runnables);
        }

        searchWriteDestination.close(true);
      }
    }

    for (IndexAccessor indexAccessor : _indexAccessors.values()) {
      indexAccessor.close();
    }
  }
  public static Map<String, String> getClusterServerInfo(String clusterNodeId) throws Exception {

    List<ClusterNode> clusterNodes = ClusterExecutorUtil.getClusterNodes();

    ClusterNode clusterNode = null;

    for (ClusterNode curClusterNode : clusterNodes) {
      String curClusterNodeId = curClusterNode.getClusterNodeId();

      if (curClusterNodeId.equals(clusterNodeId)) {
        clusterNode = curClusterNode;

        break;
      }
    }

    if (clusterNode == null) {
      return null;
    }

    try {
      if (clusterNode.equals(ClusterExecutorUtil.getLocalClusterNode())) {
        return getServerInfo();
      }

      ClusterRequest clusterRequest =
          ClusterRequest.createUnicastRequest(_getServerInfoMethodHandler, clusterNodeId);

      FutureClusterResponses futureClusterResponses = ClusterExecutorUtil.execute(clusterRequest);

      ClusterNodeResponses clusterNodeResponses =
          futureClusterResponses.get(20000, TimeUnit.MILLISECONDS);

      ClusterNodeResponse clusterNodeResponse =
          clusterNodeResponses.getClusterResponse(clusterNode);

      return (Map<String, String>) clusterNodeResponse.getResult();
    } catch (Exception e) {
      _log.error(e, e);

      throw e;
    }
  }
  private ObjectValuePair<String, URL> _getBootupClusterNodeObjectValuePair(Address bootupAddress) {

    ClusterRequest clusterRequest =
        ClusterRequest.createUnicastRequest(
            new MethodHandler(_createTokenMethodKey, _CLUSTER_LINK_NODE_BOOTUP_RESPONSE_TIMEOUT),
            bootupAddress);

    FutureClusterResponses futureClusterResponses = ClusterExecutorUtil.execute(clusterRequest);

    BlockingQueue<ClusterNodeResponse> clusterNodeResponses =
        futureClusterResponses.getPartialResults();

    try {
      ClusterNodeResponse clusterNodeResponse =
          clusterNodeResponses.poll(
              _CLUSTER_LINK_NODE_BOOTUP_RESPONSE_TIMEOUT, TimeUnit.MILLISECONDS);

      ClusterNode clusterNode = clusterNodeResponse.getClusterNode();

      InetSocketAddress inetSocketAddress = clusterNode.getPortalInetSocketAddress();

      if (inetSocketAddress == null) {
        StringBundler sb = new StringBundler(6);

        sb.append("Invalid cluster node InetSocketAddress ");
        sb.append(". The InetSocketAddress is set by the first ");
        sb.append("request or configured in portal.properties by the");
        sb.append("properties ");
        sb.append("\"portal.instance.http.inet.socket.address\" and ");
        sb.append("\"portal.instance.https.inet.socket.address\".");

        throw new Exception(sb.toString());
      }

      InetAddress inetAddress = inetSocketAddress.getAddress();

      String fileName = PortalUtil.getPathContext();

      if (!fileName.endsWith(StringPool.SLASH)) {
        fileName = fileName.concat(StringPool.SLASH);
      }

      fileName = fileName.concat("lucene/dump");

      URL url =
          new URL(_protocol, inetAddress.getHostAddress(), inetSocketAddress.getPort(), fileName);

      String transientToken = (String) clusterNodeResponse.getResult();

      return new ObjectValuePair<String, URL>(transientToken, url);
    } catch (Exception e) {
      throw new SystemException(e);
    }
  }
Exemple #10
0
  private ClusterSchedulerEngine _getClusterSchedulerEngine(
      boolean master, int memoryClusterJobs, int persistentJobs) throws Exception {

    MockSchedulerEngine mockSchedulerEngine =
        new MockSchedulerEngine(memoryClusterJobs, persistentJobs);

    MockClusterExecutor mockClusterExecutor = new MockClusterExecutor(true, mockSchedulerEngine);

    ClusterExecutorUtil clusterExecutorUtil = new ClusterExecutorUtil();

    clusterExecutorUtil.setClusterExecutor(mockClusterExecutor);

    ClusterSchedulerEngine clusterSchedulerEngine = new ClusterSchedulerEngine(mockSchedulerEngine);

    Address masterAddress = null;

    if (master) {
      masterAddress = ClusterExecutorUtil.getLocalClusterNodeAddress();
    } else {
      masterAddress = MockClusterExecutor._anotherAddress;
    }

    MockLockLocalService.setLock(AddressSerializerUtil.serialize(masterAddress));

    SchedulerEngineHelperImpl schedulerEngineHelperImpl = new SchedulerEngineHelperImpl();

    schedulerEngineHelperImpl.setSchedulerEngine(clusterSchedulerEngine);

    SchedulerEngineHelperUtil schedulerEngineHelperUtil = new SchedulerEngineHelperUtil();

    schedulerEngineHelperUtil.setSchedulerEngineHelper(schedulerEngineHelperImpl);

    clusterSchedulerEngine.initialize();

    clusterSchedulerEngine.start();

    return clusterSchedulerEngine;
  }
  private FutureClusterResponses doAction() throws ManageActionException, SystemException {

    MethodHandler manageActionMethodHandler =
        PortalManagerUtil.createManageActionMethodHandler(_manageAction);

    ClusterRequest clusterRequest = null;

    if (_clusterGroup.isWholeCluster()) {
      clusterRequest = ClusterRequest.createMulticastRequest(manageActionMethodHandler);
    } else {
      verifyClusterGroup();

      clusterRequest =
          ClusterRequest.createUnicastRequest(
              manageActionMethodHandler, _clusterGroup.getClusterNodeIdsArray());
    }

    return ClusterExecutorUtil.execute(clusterRequest);
  }
  public static void registerOrder(HttpServletRequest request) {
    String orderUuid = ParamUtil.getString(request, "orderUuid");
    String productEntryName = ParamUtil.getString(request, "productEntryName");
    int maxServers = ParamUtil.getInteger(request, "maxServers");

    List<ClusterNode> clusterNodes = ClusterExecutorUtil.getClusterNodes();

    if ((clusterNodes.size() <= 1)
        || Validator.isNull(productEntryName)
        || Validator.isNull(orderUuid)) {

      Map<String, Object> attributes = registerOrder(orderUuid, productEntryName, maxServers);

      for (Map.Entry<String, Object> entry : attributes.entrySet()) {
        request.setAttribute(entry.getKey(), entry.getValue());
      }
    } else {
      for (ClusterNode clusterNode : clusterNodes) {
        boolean register =
            ParamUtil.getBoolean(request, clusterNode.getClusterNodeId() + "_register");

        if (!register) {
          continue;
        }

        try {
          _registerClusterOrder(request, clusterNode, orderUuid, productEntryName, maxServers);
        } catch (Exception e) {
          _log.error(e, e);

          InetAddress inetAddress = clusterNode.getInetAddress();

          String message = "Error contacting " + inetAddress.getHostName();

          if (clusterNode.getPort() != -1) {
            message += StringPool.COLON + clusterNode.getPort();
          }

          request.setAttribute(clusterNode.getClusterNodeId() + "_ERROR_MESSAGE", message);
        }
      }
    }
  }
  private LuceneHelperImpl() {
    if (PropsValues.INDEX_ON_STARTUP && PropsValues.INDEX_WITH_THREAD) {
      _luceneIndexThreadPoolExecutor =
          PortalExecutorManagerUtil.getPortalExecutor(LuceneHelperImpl.class.getName());
    }

    if (isLoadIndexFromClusterEnabled()) {
      _loadIndexClusterEventListener = new LoadIndexClusterEventListener();

      ClusterExecutorUtil.addClusterEventListener(_loadIndexClusterEventListener);
    }

    BooleanQuery.setMaxClauseCount(_LUCENE_BOOLEAN_QUERY_CLAUSE_MAX_SIZE);

    if (StringUtil.equalsIgnoreCase(Http.HTTPS, PropsValues.WEB_SERVER_PROTOCOL)) {

      _protocol = Http.HTTPS;
    } else {
      _protocol = Http.HTTP;
    }
  }
  protected String importTheme(LayoutSet layoutSet, InputStream themeZip) throws Exception {

    ThemeLoader themeLoader = ThemeLoaderFactory.getDefaultThemeLoader();

    if (themeLoader == null) {
      _log.error("No theme loaders are deployed");

      return null;
    }

    ZipReader zipReader = ZipReaderFactoryUtil.getZipReader(themeZip);

    String lookAndFeelXML = zipReader.getEntryAsString("liferay-look-and-feel.xml");

    String themeId = String.valueOf(layoutSet.getGroupId());

    if (layoutSet.isPrivateLayout()) {
      themeId += "-private";
    } else {
      themeId += "-public";
    }

    if (PropsValues.THEME_LOADER_NEW_THEME_ID_ON_IMPORT) {
      Date now = new Date();

      themeId += "-" + Time.getShortTimestamp(now);
    }

    String themeName = themeId;

    lookAndFeelXML =
        StringUtil.replace(
            lookAndFeelXML,
            new String[] {"[$GROUP_ID$]", "[$THEME_ID$]", "[$THEME_NAME$]"},
            new String[] {String.valueOf(layoutSet.getGroupId()), themeId, themeName});

    FileUtil.deltree(themeLoader.getFileStorage() + StringPool.SLASH + themeId);

    List<String> zipEntries = zipReader.getEntries();

    for (String zipEntry : zipEntries) {
      String key = zipEntry;

      if (key.equals("liferay-look-and-feel.xml")) {
        FileUtil.write(
            themeLoader.getFileStorage() + StringPool.SLASH + themeId + StringPool.SLASH + key,
            lookAndFeelXML.getBytes());
      } else {
        InputStream is = zipReader.getEntryAsInputStream(zipEntry);

        FileUtil.write(
            themeLoader.getFileStorage() + StringPool.SLASH + themeId + StringPool.SLASH + key, is);
      }
    }

    themeLoader.loadThemes();

    ClusterRequest clusterRequest =
        ClusterRequest.createMulticastRequest(_loadThemesMethodHandler, true);

    clusterRequest.setFireAndForget(true);

    ClusterExecutorUtil.execute(clusterRequest);

    themeId += PortletConstants.WAR_SEPARATOR + themeLoader.getServletContextName();

    return PortalUtil.getJsSafePortletId(themeId);
  }
  protected void doRun(String[] ids) throws Exception {

    // Print release information

    System.out.println("Starting " + ReleaseInfo.getReleaseInfo());

    // Portal resiliency

    DistributedRegistry.registerDistributed(
        ComponentConstants.COMPONENT_CONTEXT, Direction.DUPLEX, MatchType.POSTFIX);
    DistributedRegistry.registerDistributed(
        MimeResponse.MARKUP_HEAD_ELEMENT, Direction.DUPLEX, MatchType.EXACT);
    DistributedRegistry.registerDistributed(
        PortletRequest.LIFECYCLE_PHASE, Direction.DUPLEX, MatchType.EXACT);
    DistributedRegistry.registerDistributed(WebKeys.class);

    Intraband intraband = MPIHelperUtil.getIntraband();

    intraband.registerDatagramReceiveHandler(
        SystemDataType.MAILBOX.getValue(), new MailboxDatagramReceiveHandler());

    MessageBus messageBus = (MessageBus) PortalBeanLocatorUtil.locate(MessageBus.class.getName());

    intraband.registerDatagramReceiveHandler(
        SystemDataType.MESSAGE.getValue(), new MessageDatagramReceiveHandler(messageBus));

    intraband.registerDatagramReceiveHandler(
        SystemDataType.PROXY.getValue(), new IntrabandProxyDatagramReceiveHandler());

    intraband.registerDatagramReceiveHandler(
        SystemDataType.RPC.getValue(), new RPCDatagramReceiveHandler());

    // Shutdown hook

    if (_log.isDebugEnabled()) {
      _log.debug("Add shutdown hook");
    }

    Runtime runtime = Runtime.getRuntime();

    runtime.addShutdownHook(new Thread(new ShutdownHook()));

    // Template manager

    if (_log.isDebugEnabled()) {
      _log.debug("Initialize template manager");
    }

    TemplateManagerUtil.init();

    // Indexers

    IndexerRegistryUtil.register(new MBMessageIndexer());
    IndexerRegistryUtil.register(new PluginPackageIndexer());

    // Upgrade

    if (_log.isDebugEnabled()) {
      _log.debug("Upgrade database");
    }

    DBUpgrader.upgrade();

    // Clear locks

    if (_log.isDebugEnabled()) {
      _log.debug("Clear locks");
    }

    try {
      LockLocalServiceUtil.clear();
    } catch (Exception e) {
      if (_log.isWarnEnabled()) {
        _log.warn("Unable to clear locks because Lock table does not exist");
      }
    }

    // Messaging

    if (_log.isDebugEnabled()) {
      _log.debug("Initialize message bus");
    }

    MessageSender messageSender =
        (MessageSender) PortalBeanLocatorUtil.locate(MessageSender.class.getName());
    SynchronousMessageSender synchronousMessageSender =
        (SynchronousMessageSender)
            PortalBeanLocatorUtil.locate(SynchronousMessageSender.class.getName());

    MessageBusUtil.init(
        DoPrivilegedUtil.wrap(messageBus),
        DoPrivilegedUtil.wrap(messageSender),
        DoPrivilegedUtil.wrap(synchronousMessageSender));

    // Cluster executor

    ClusterExecutorUtil.initialize();

    if (!SPIUtil.isSPI()) {
      ClusterMasterExecutorUtil.initialize();
    }

    // Ehache bootstrap

    EhcacheStreamBootstrapCacheLoader.start();

    // Scheduler

    if (_log.isDebugEnabled()) {
      _log.debug("Initialize scheduler engine lifecycle");
    }

    SchedulerEngineHelperUtil.initialize();

    // Verify

    if (_log.isDebugEnabled()) {
      _log.debug("Verify database");
    }

    DBUpgrader.verify();

    // Background tasks

    if (!ClusterMasterExecutorUtil.isEnabled()) {
      BackgroundTaskLocalServiceUtil.cleanUpBackgroundTasks();
    }

    // Liferay JspFactory

    JspFactorySwapper.swap();

    // Jericho

    CachedLoggerProvider.install();
  }
    public void run() {
      _countDownLatch.countDown();

      String logPrefix = StringPool.BLANK;

      if (_log.isInfoEnabled()) {
        Thread currentThread = Thread.currentThread();

        if (_master) {
          logPrefix =
              "Monitor thread name "
                  + currentThread.getName()
                  + " with thread ID "
                  + currentThread.getId();
        } else {
          logPrefix =
              "Thread name " + currentThread.getName() + " with thread ID " + currentThread.getId();
        }
      }

      if (!_master && _log.isInfoEnabled()) {
        _log.info(logPrefix + " synchronized on latch. Waiting for others.");
      }

      try {
        if (_master) {
          _countDownLatch.await();
        } else {
          boolean result =
              _countDownLatch.await(
                  PropsValues.LUCENE_CLUSTER_INDEX_LOADING_SYNC_TIMEOUT, TimeUnit.MILLISECONDS);

          if (!result) {
            _log.error(
                logPrefix + " timed out. You may need to " + "re-trigger a reindex process.");
          }
        }
      } catch (InterruptedException ie) {
        if (_master) {
          _log.error(
              logPrefix + " was interrupted. Skip cluster index " + "loading notification.", ie);

          return;
        } else {
          _log.error(
              logPrefix + " was interrupted. You may need to " + "re-trigger a reindex process.",
              ie);
        }
      }

      if (_master) {
        Address localClusterNodeAddress = ClusterExecutorUtil.getLocalClusterNodeAddress();

        ClusterRequest clusterRequest =
            ClusterRequest.createMulticastRequest(
                new MethodHandler(
                    _loadIndexesFromClusterMethodKey, _companyIds, localClusterNodeAddress),
                true);

        try {
          ClusterExecutorUtil.execute(clusterRequest);
        } catch (SystemException se) {
          _log.error("Unable to notify peers to start index loading", se);
        }

        if (_log.isInfoEnabled()) {
          _log.info(logPrefix + " unlocked latch. Notified peers to " + "start index loading.");
        }
      }
    }