You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

CeTasksMBeanImpl.java 4.6KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135
  1. /*
  2. * SonarQube
  3. * Copyright (C) 2009-2019 SonarSource SA
  4. * mailto:info AT sonarsource DOT com
  5. *
  6. * This program is free software; you can redistribute it and/or
  7. * modify it under the terms of the GNU Lesser General Public
  8. * License as published by the Free Software Foundation; either
  9. * version 3 of the License, or (at your option) any later version.
  10. *
  11. * This program is distributed in the hope that it will be useful,
  12. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  14. * Lesser General Public License for more details.
  15. *
  16. * You should have received a copy of the GNU Lesser General Public License
  17. * along with this program; if not, write to the Free Software Foundation,
  18. * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
  19. */
  20. package org.sonar.ce.monitoring;
  21. import java.util.List;
  22. import java.util.Optional;
  23. import java.util.Set;
  24. import java.util.stream.Collectors;
  25. import org.picocontainer.Startable;
  26. import org.sonar.ce.configuration.CeConfiguration;
  27. import org.sonar.ce.taskprocessor.CeWorker;
  28. import org.sonar.ce.taskprocessor.CeWorkerController;
  29. import org.sonar.ce.taskprocessor.CeWorkerFactory;
  30. import org.sonar.process.Jmx;
  31. import org.sonar.process.systeminfo.SystemInfoSection;
  32. import org.sonar.process.systeminfo.protobuf.ProtobufSystemInfo;
  33. public class CeTasksMBeanImpl implements CeTasksMBean, Startable, SystemInfoSection {
  34. private final CEQueueStatus queueStatus;
  35. private final CeConfiguration ceConfiguration;
  36. private final CeWorkerFactory ceWorkerFactory;
  37. private final CeWorkerController ceWorkerController;
  38. public CeTasksMBeanImpl(CEQueueStatus queueStatus, CeConfiguration ceConfiguration, CeWorkerFactory ceWorkerFactory, CeWorkerController CeWorkerController) {
  39. this.queueStatus = queueStatus;
  40. this.ceConfiguration = ceConfiguration;
  41. this.ceWorkerFactory = ceWorkerFactory;
  42. this.ceWorkerController = CeWorkerController;
  43. }
  44. @Override
  45. public void start() {
  46. Jmx.register(OBJECT_NAME, this);
  47. }
  48. /**
  49. * Unregister, if needed
  50. */
  51. @Override
  52. public void stop() {
  53. Jmx.unregister(OBJECT_NAME);
  54. }
  55. @Override
  56. public long getPendingCount() {
  57. return queueStatus.getPendingCount();
  58. }
  59. @Override
  60. public Optional<Long> getLongestTimePending() {
  61. return queueStatus.getLongestTimePending();
  62. }
  63. @Override
  64. public long getInProgressCount() {
  65. return queueStatus.getInProgressCount();
  66. }
  67. @Override
  68. public long getErrorCount() {
  69. return queueStatus.getErrorCount();
  70. }
  71. @Override
  72. public long getSuccessCount() {
  73. return queueStatus.getSuccessCount();
  74. }
  75. @Override
  76. public long getProcessingTime() {
  77. return queueStatus.getProcessingTime();
  78. }
  79. @Override
  80. public int getWorkerMaxCount() {
  81. return ceConfiguration.getWorkerMaxCount();
  82. }
  83. @Override
  84. public int getWorkerCount() {
  85. return ceConfiguration.getWorkerCount();
  86. }
  87. @Override
  88. public List<String> getWorkerUuids() {
  89. Set<CeWorker> workers = ceWorkerFactory.getWorkers();
  90. return workers.stream()
  91. .map(CeWorker::getUUID)
  92. .sorted()
  93. .collect(Collectors.toList());
  94. }
  95. @Override
  96. public List<String> getEnabledWorkerUuids() {
  97. Set<CeWorker> workers = ceWorkerFactory.getWorkers();
  98. return workers.stream()
  99. .filter(ceWorkerController::isEnabled)
  100. .map(CeWorker::getUUID)
  101. .sorted()
  102. .collect(Collectors.toList());
  103. }
  104. @Override
  105. public ProtobufSystemInfo.Section toProtobuf() {
  106. ProtobufSystemInfo.Section.Builder builder = ProtobufSystemInfo.Section.newBuilder();
  107. builder.setName("Compute Engine Tasks");
  108. builder.addAttributesBuilder().setKey("Pending").setLongValue(getPendingCount()).build();
  109. builder.addAttributesBuilder().setKey("Longest Time Pending (ms)").setLongValue(getLongestTimePending().orElse(0L)).build();
  110. builder.addAttributesBuilder().setKey("In Progress").setLongValue(getInProgressCount()).build();
  111. builder.addAttributesBuilder().setKey("Processed With Error").setLongValue(getErrorCount()).build();
  112. builder.addAttributesBuilder().setKey("Processed With Success").setLongValue(getSuccessCount()).build();
  113. builder.addAttributesBuilder().setKey("Processing Time (ms)").setLongValue(getProcessingTime()).build();
  114. builder.addAttributesBuilder().setKey("Worker Count").setLongValue(getWorkerCount()).build();
  115. builder.addAttributesBuilder().setKey("Max Worker Count").setLongValue(getWorkerMaxCount()).build();
  116. builder.addAttributesBuilder().setKey("Workers Paused").setBooleanValue(queueStatus.areWorkersPaused()).build();
  117. return builder.build();
  118. }
  119. }