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.

StandaloneCeDistributedInformation.java 2.9KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899
  1. /*
  2. * SonarQube
  3. * Copyright (C) 2009-2021 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;
  21. import java.util.Set;
  22. import java.util.concurrent.TimeUnit;
  23. import java.util.concurrent.locks.Condition;
  24. import java.util.concurrent.locks.Lock;
  25. import org.sonar.ce.taskprocessor.CeWorker;
  26. import org.sonar.ce.taskprocessor.CeWorkerFactory;
  27. import static com.google.common.base.Preconditions.checkState;
  28. import static org.sonar.core.util.stream.MoreCollectors.toSet;
  29. /**
  30. * Provide the set of worker's UUID in a non clustered SonarQube instance
  31. */
  32. public class StandaloneCeDistributedInformation implements CeDistributedInformation {
  33. private final CeWorkerFactory ceCeWorkerFactory;
  34. private Set<String> workerUUIDs;
  35. private Lock cleanJobLock = new NonConcurrentLock();
  36. public StandaloneCeDistributedInformation(CeWorkerFactory ceCeWorkerFactory) {
  37. this.ceCeWorkerFactory = ceCeWorkerFactory;
  38. }
  39. @Override
  40. public Set<String> getWorkerUUIDs() {
  41. checkState(workerUUIDs != null, "Invalid call, broadcastWorkerUUIDs() must be called first.");
  42. return workerUUIDs;
  43. }
  44. @Override
  45. public void broadcastWorkerUUIDs() {
  46. Set<CeWorker> workers = ceCeWorkerFactory.getWorkers();
  47. workerUUIDs = workers.stream().map(CeWorker::getUUID).collect(toSet(workers.size()));
  48. }
  49. /**
  50. * Since StandaloneCeDistributedInformation in fact does not provide any distribution support, the lock returned by
  51. * this method provides no concurrency support at all.
  52. */
  53. @Override
  54. public Lock acquireCleanJobLock() {
  55. return cleanJobLock;
  56. }
  57. private static class NonConcurrentLock implements Lock {
  58. @Override
  59. public void lock() {
  60. // return immediately and never block
  61. }
  62. @Override
  63. public void lockInterruptibly() {
  64. // return immediately and never block
  65. }
  66. @Override
  67. public boolean tryLock() {
  68. // always succeed
  69. return true;
  70. }
  71. @Override
  72. public boolean tryLock(long time, TimeUnit unit) {
  73. // always succeed
  74. return true;
  75. }
  76. @Override
  77. public void unlock() {
  78. // nothing to do
  79. }
  80. @Override
  81. public Condition newCondition() {
  82. throw new UnsupportedOperationException("newCondition not supported");
  83. }
  84. }
  85. }