protected ClusterNodeResponse executeClusterRequest(ClusterRequest clusterRequest) {

    Serializable payload = clusterRequest.getPayload();

    if (!(payload instanceof MethodHandler)) {
      return ClusterNodeResponse.createExceptionClusterNodeResponse(
          _localClusterNode,
          clusterRequest.getUuid(),
          new ClusterException("Payload is not of type " + MethodHandler.class.getName()));
    }

    MethodHandler methodHandler = (MethodHandler) payload;

    ClusterInvokeThreadLocal.setEnabled(false);

    try {
      return ClusterNodeResponse.createResultClusterNodeResponse(
          _localClusterNode, clusterRequest.getUuid(), methodHandler.invoke());
    } catch (Exception e) {
      return ClusterNodeResponse.createExceptionClusterNodeResponse(
          _localClusterNode, clusterRequest.getUuid(), e);
    } finally {
      ClusterInvokeThreadLocal.setEnabled(true);
    }
  }
  protected List<Address> prepareAddresses(ClusterRequest clusterRequest) {
    boolean isMulticast = clusterRequest.isMulticast();

    List<Address> addresses = null;

    if (isMulticast) {
      addresses = getAddresses(_controlJChannel);
    } else {
      addresses = new ArrayList<>();

      Collection<String> clusterNodeIds = clusterRequest.getTargetClusterNodeIds();

      if (clusterNodeIds != null) {
        for (String clusterNodeId : clusterNodeIds) {
          Address address = _clusterNodeAddresses.get(clusterNodeId);

          addresses.add(address);
        }
      }
    }

    if (clusterRequest.isSkipLocal()) {
      addresses.remove(_localAddress);
    }

    return addresses;
  }
  @AdviseWith(adviceClasses = {EnableClusterLinkAdvice.class})
  @Test
  public void testInvoke6() throws Exception {
    ClusterExecutorImpl clusterExecutorImpl1 = null;
    ClusterExecutorImpl clusterExecutorImpl2 = null;

    try {
      clusterExecutorImpl1 = getClusterExecutorImpl(false, true);
      clusterExecutorImpl2 = getClusterExecutorImpl(false, true);

      MethodHandler methodHandler = new MethodHandler(testMethod4MethodKey);

      Address address = clusterExecutorImpl2.getLocalClusterNodeAddress();

      ClusterRequest clusterRequest = ClusterRequest.createUnicastRequest(methodHandler, address);

      clusterRequest.setBeanIdentifier(BEAN_IDENTIFIER);

      FutureClusterResponses futureClusterResponses = clusterExecutorImpl1.execute(clusterRequest);

      assertFutureClusterResponsesWithoutException(
          futureClusterResponses.get(),
          clusterRequest.getUuid(),
          SERIALIZABLE_RETRUN_VALUE,
          address);
    } finally {
      if (clusterExecutorImpl1 != null) {
        clusterExecutorImpl1.destroy();
      }

      if (clusterExecutorImpl2 != null) {
        clusterExecutorImpl2.destroy();
      }
    }
  }
  @AdviseWith(adviceClasses = {EnableClusterLinkAdvice.class})
  @Test
  public void testInvoke3() throws Exception {
    ClusterExecutorImpl clusterExecutorImpl1 = null;
    ClusterExecutorImpl clusterExecutorImpl2 = null;

    try {
      clusterExecutorImpl1 = getClusterExecutorImpl(false, false);
      clusterExecutorImpl2 = getClusterExecutorImpl(false, false);

      MethodHandler methodHandler = new MethodHandler(testMethod2MethodKey);

      Address address = clusterExecutorImpl2.getLocalClusterNodeAddress();

      ClusterRequest clusterRequest = ClusterRequest.createUnicastRequest(methodHandler, address);

      FutureClusterResponses futureClusterResponses = clusterExecutorImpl1.execute(clusterRequest);

      assertFutureClusterResponsesWithException(
          futureClusterResponses,
          clusterRequest.getUuid(),
          address,
          "Return value is not serializable");
    } finally {
      if (clusterExecutorImpl1 != null) {
        clusterExecutorImpl1.destroy();
      }

      if (clusterExecutorImpl2 != null) {
        clusterExecutorImpl2.destroy();
      }
    }
  }
  @AdviseWith(adviceClasses = {EnableClusterLinkAdvice.class})
  @Test
  public void testInvoke2() throws Exception {
    ClusterExecutorImpl clusterExecutorImpl1 = null;
    ClusterExecutorImpl clusterExecutorImpl2 = null;

    try {
      clusterExecutorImpl1 = getClusterExecutorImpl(false, false);
      clusterExecutorImpl2 = getClusterExecutorImpl(false, false);

      String timestamp = String.valueOf(System.currentTimeMillis());

      MethodHandler methodHandler = new MethodHandler(testMethod1MethodKey, timestamp);

      Address address = clusterExecutorImpl2.getLocalClusterNodeAddress();

      ClusterRequest clusterRequest = ClusterRequest.createUnicastRequest(methodHandler, address);

      FutureClusterResponses futureClusterResponses = clusterExecutorImpl1.execute(clusterRequest);

      assertFutureClusterResponsesWithoutException(
          futureClusterResponses.get(), clusterRequest.getUuid(), timestamp, address);
    } finally {
      if (clusterExecutorImpl1 != null) {
        clusterExecutorImpl1.destroy();
      }

      if (clusterExecutorImpl2 != null) {
        clusterExecutorImpl2.destroy();
      }
    }
  }
  @AdviseWith(adviceClasses = {EnableClusterLinkAdvice.class})
  @Test
  public void testInvoke5() throws Exception {
    ClusterExecutorImpl clusterExecutorImpl1 = null;
    ClusterExecutorImpl clusterExecutorImpl2 = null;

    try {
      clusterExecutorImpl1 = getClusterExecutorImpl(false, false);
      clusterExecutorImpl2 = getClusterExecutorImpl(false, false);

      Address address = clusterExecutorImpl2.getLocalClusterNodeAddress();

      ClusterRequest clusterRequest = ClusterRequest.createUnicastRequest(null, address);

      FutureClusterResponses futureClusterResponses = clusterExecutorImpl1.execute(clusterRequest);

      assertFutureClusterResponsesWithException(
          futureClusterResponses,
          clusterRequest.getUuid(),
          address,
          "Payload is not of type " + MethodHandler.class.getName());
    } finally {
      if (clusterExecutorImpl1 != null) {
        clusterExecutorImpl1.destroy();
      }

      if (clusterExecutorImpl2 != null) {
        clusterExecutorImpl2.destroy();
      }
    }
  }
  @Override
  public FutureClusterResponses execute(ClusterRequest clusterRequest) {
    if (!isEnabled()) {
      return null;
    }

    List<Address> addresses = prepareAddresses(clusterRequest);

    Set<String> clusterNodeIds = new HashSet<>();

    for (Address address : addresses) {
      ClusterNode clusterNode = _liveInstances.get(address);

      if (clusterNode != null) {
        clusterNodeIds.add(clusterNode.getClusterNodeId());
      }
    }

    FutureClusterResponses futureClusterResponses = new FutureClusterResponses(clusterNodeIds);

    if (!clusterRequest.isFireAndForget()) {
      String uuid = clusterRequest.getUuid();

      _futureClusterResponses.put(uuid, futureClusterResponses);
    }

    if (addresses.remove(_localAddress)) {
      ClusterNodeResponse clusterNodeResponse = executeClusterRequest(clusterRequest);

      if (!clusterRequest.isFireAndForget()) {
        futureClusterResponses.addClusterNodeResponse(clusterNodeResponse);
      }
    }

    if (clusterRequest.isMulticast()) {
      try {
        _controlJChannel.send(null, clusterRequest);
      } catch (Exception e) {
        throw new SystemException("Unable to send multicast request", e);
      }
    } else {
      for (Address address : addresses) {
        org.jgroups.Address jGroupsAddress = (org.jgroups.Address) address.getRealAddress();

        try {
          _controlJChannel.send(jGroupsAddress, clusterRequest);
        } catch (Exception e) {
          throw new SystemException("Unable to send unicast request", e);
        }
      }
    }

    return futureClusterResponses;
  }
  @Override
  public void portalLocalInetSocketAddressConfigured(
      InetSocketAddress inetSocketAddress, boolean secure) {

    if (!isEnabled() || (_localClusterNode.getPortalProtocol() != null)) {
      return;
    }

    try {
      _localClusterNode.setPortalInetSocketAddress(inetSocketAddress);

      if (secure) {
        _localClusterNode.setPortalProtocol(Http.HTTPS);
      } else {
        _localClusterNode.setPortalProtocol(Http.HTTP);
      }

      memberJoined(_localAddress, _localClusterNode);

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

      _controlJChannel.send(null, clusterRequest);
    } catch (Exception e) {
      _log.error("Unable to determine configure node port", e);
    }
  }
  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));
  }
  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());
    }
  }
  protected void sendNotifyRequest() {
    ClusterRequest clusterRequest = ClusterRequest.createMulticastRequest(_localClusterNode, true);

    try {
      _controlJChannel.send(null, clusterRequest);
    } catch (Exception e) {
      _log.error("Unable to send notify message", e);
    }
  }
  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);
  }
  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);
    }
  }
  @AdviseWith(adviceClasses = {EnableClusterLinkAdvice.class})
  @Test
  public void testInvoke9() throws Exception {
    ClusterExecutorImpl clusterExecutorImpl1 = null;
    ClusterExecutorImpl clusterExecutorImpl2 = null;

    try {
      clusterExecutorImpl1 = getClusterExecutorImpl(false, true);
      clusterExecutorImpl2 = getClusterExecutorImpl(false, true);

      MethodHandler methodHandler =
          new MethodHandler(new MethodKey(TestBean.class, "nonexistentMethod"));

      Address address = clusterExecutorImpl2.getLocalClusterNodeAddress();

      ClusterRequest clusterRequest = ClusterRequest.createUnicastRequest(methodHandler, address);

      clusterRequest.setServletContextName(SERVLET_CONTEXT_NAME);

      List<LogRecord> logRecords =
          JDKLoggerTestUtil.configureJDKLogger(
              ClusterRequestReceiver.class.getName(), Level.SEVERE);

      FutureClusterResponses futureClusterResponses = clusterExecutorImpl1.execute(clusterRequest);

      assertFutureClusterResponsesWithException(
          futureClusterResponses, clusterRequest.getUuid(), address, null);

      assertLogger(
          logRecords, "Unable to invoke method " + methodHandler, NoSuchMethodException.class);
    } finally {
      if (clusterExecutorImpl1 != null) {
        clusterExecutorImpl1.destroy();
      }

      if (clusterExecutorImpl2 != null) {
        clusterExecutorImpl2.destroy();
      }
    }
  }
Beispiel #15
0
    @Override
    public FutureClusterResponses execute(ClusterRequest clusterRequest) {
      List<Address> addresses = new ArrayList<Address>();

      Collection<Address> clusterNodeAddresses = clusterRequest.getTargetClusterNodeAddresses();

      if (clusterNodeAddresses != null) {
        addresses.addAll(clusterNodeAddresses);
      }

      FutureClusterResponses futureClusterResponses = new FutureClusterResponses(addresses);

      for (Address address : addresses) {
        ClusterNodeResponse clusterNodeResponse = new ClusterNodeResponse();

        clusterNodeResponse.setAddress(address);
        clusterNodeResponse.setClusterMessageType(ClusterMessageType.EXECUTE);
        clusterNodeResponse.setMulticast(clusterRequest.isMulticast());
        clusterNodeResponse.setUuid(clusterRequest.getUuid());

        MockAddress mockAddress = (MockAddress) address.getRealAddress();

        try {
          clusterNodeResponse.setClusterNode(
              new ClusterNode(
                  String.valueOf(mockAddress.getTimestamp()), InetAddress.getLocalHost()));

          clusterNodeResponse.setResult(_invoke(clusterRequest.getMethodHandler()));
        } catch (Exception e) {
        }

        futureClusterResponses.addClusterNodeResponse(clusterNodeResponse);
      }

      return futureClusterResponses;
    }
  @Override
  protected void doReceive(Object messagePayload, Address srcAddress) {
    ClusterChannel clusterChannel = _clusterExecutorImpl.getClusterChannel();

    if (srcAddress.equals(clusterChannel.getLocalAddress())) {
      return;
    }

    try {
      if (messagePayload instanceof ClusterRequest) {
        ClusterRequest clusterRequest = (ClusterRequest) messagePayload;

        Serializable responsePayload =
            _clusterExecutorImpl.handleReceivedClusterRequest(clusterRequest);

        if (clusterRequest.isFireAndForget()) {
          return;
        }

        try {
          clusterChannel.sendUnicastMessage(responsePayload, srcAddress);
        } catch (Throwable t) {
          _log.error("Unable to send message " + responsePayload, t);
        }
      } else if (messagePayload instanceof ClusterNodeResponse) {
        _clusterExecutorImpl.handleReceivedClusterNodeResponse(
            (ClusterNodeResponse) messagePayload);
      } else if (_log.isWarnEnabled()) {
        _log.warn("Unable to process message content of type " + messagePayload.getClass());
      }
    } finally {
      ThreadLocalCacheManager.clearAll(Lifecycle.REQUEST);

      CentralizedThreadLocal.clearShortLivedThreadLocals();
    }
  }
  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;
    }
  }
  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);
  }
    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.");
        }
      }
    }