mirror of https://github.com/OpenIdentityPlatform/OpenDJ.git

Valery Kharseko
2 days ago 80481f756d71bd58b4bda627758dcd774e9d5dcd
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
/*
 * The contents of this file are subject to the terms of the Common Development and
 * Distribution License (the License). You may not use this file except in compliance with the
 * License.
 *
 * You can obtain a copy of the License at legal/CDDLv1.0.txt. See the License for the
 * specific language governing permission and limitations under the License.
 *
 * When distributing Covered Software, include this CDDL Header Notice in each file and include
 * the License file at legal/CDDLv1.0.txt. If applicable, add the following below the CDDL
 * Header, with the fields enclosed by brackets [] replaced by your own identifying
 * information: "Portions copyright [year] [name of copyright owner]".
 *
 * Copyright 2026 3A Systems, LLC.
 */
package org.opends.server.replication.plugin;
 
import static org.assertj.core.api.Assertions.*;
import static org.opends.server.TestCaseUtils.*;
 
import java.util.ArrayList;
import java.util.List;
import java.util.SortedSet;
import java.util.TreeSet;
 
import org.forgerock.opendj.ldap.DN;
import org.opends.server.TestCaseUtils;
import org.opends.server.backends.MemoryBackend;
import org.opends.server.replication.ReplicationTestCase;
import org.opends.server.replication.server.ReplServerFakeConfiguration;
import org.opends.server.replication.server.ReplicationServer;
import org.testng.annotations.AfterMethod;
import org.testng.annotations.BeforeMethod;
import org.testng.annotations.Test;
 
/**
 * Tests that {@link MultimasterReplication#forEachDomain} gives every domain its turn
 * whatever one of them threw, and reports the failures the way it says it does: the first
 * is thrown once the loop is over, the others are suppressed under it where it records
 * suppression, and an instance thrown by two domains - the error a JVM out of memory hands
 * out as often as it is asked for one - is thrown once and suppresses nothing (issue #986).
 * <p>
 * Two domains, on backends of their own, neither of them started: the action is what
 * throws, and it never touches the domain it is given.
 */
@SuppressWarnings("javadoc")
public class ForEachDomainTest extends ReplicationTestCase
{
  private static final int RS_ID = 613;
  private static final String SECOND_BACKEND_ID = "test2";
  private static final String SECOND_ROOT_DN_STRING = "o=" + SECOND_BACKEND_ID;
 
  private DN firstBaseDN;
  private DN secondBaseDN;
  private ReplicationServer replicationServer;
  private LDAPReplicationDomain firstDomain;
  private LDAPReplicationDomain secondDomain;
 
  @BeforeMethod
  public void setUpLocal() throws Exception
  {
    firstBaseDN = DN.valueOf(TEST_ROOT_DN_STRING);
    secondBaseDN = DN.valueOf(SECOND_ROOT_DN_STRING);
    TestCaseUtils.initializeTestBackend(true);
    TestCaseUtils.initializeMemoryBackend(SECOND_BACKEND_ID, SECOND_ROOT_DN_STRING, true);
 
    final int rsPort = TestCaseUtils.findFreePort();
    replicationServer = new ReplicationServer(new ReplServerFakeConfiguration(
        rsPort, "forEachDomainTestDb", 0, RS_ID, 0, 100, new TreeSet<String>()));
    final SortedSet<String> replServers = new TreeSet<>();
    replServers.add("localhost:" + rsPort);
    firstDomain = MultimasterReplication.createNewDomain(new DomainFakeCfg(firstBaseDN, 1, replServers));
    secondDomain = MultimasterReplication.createNewDomain(new DomainFakeCfg(secondBaseDN, 2, replServers));
  }
 
  @AfterMethod
  public void tearDown() throws Exception
  {
    try
    {
      MultimasterReplication.deleteDomain(firstBaseDN);
      MultimasterReplication.deleteDomain(secondBaseDN);
    }
    finally
    {
      try
      {
        remove(replicationServer);
      }
      finally
      {
        final MemoryBackend backend = (MemoryBackend) getServerContext().getBackendConfigManager()
            .getLocalBackendById(SECOND_BACKEND_ID);
        if (backend != null)
        {
          backend.clearMemoryBackend();
          backend.finalizeBackend();
          getServerContext().getBackendConfigManager().deregisterLocalBackend(backend);
        }
      }
    }
  }
 
  @Test
  public void theFirstFailureIsThrownOnceTheLoopIsOverWithTheOthersSuppressedUnderIt()
  {
    final RuntimeException first = new RuntimeException("first");
    final RuntimeException second = new RuntimeException("second");
    final List<LDAPReplicationDomain> visited = new ArrayList<>();
 
    final Throwable thrown = catchThrowable(() -> MultimasterReplication.forEachDomain(domain ->
    {
      visited.add(domain);
      throw visited.size() == 1 ? first : second;
    }));
 
    assertThat(thrown).as("the failure thrown once the loop is over must be the first one met")
        .isSameAs(first);
    assertThat(thrown.getSuppressed()).as("the failure met after the first must be suppressed under it")
        .containsExactly(second);
    assertThat(visited).as("every domain must get its turn whatever the one before it threw")
        .containsExactlyInAnyOrder(firstDomain, secondDomain);
  }
 
  @Test
  public void aFailureThrownByTwoDomainsIsThrownOnceAndSuppressesNothing()
  {
    // The error a JVM out of memory prepared beforehand is handed out to every domain alike.
    final OutOfMemoryError theOneError = new OutOfMemoryError("prepared beforehand");
    final List<LDAPReplicationDomain> visited = new ArrayList<>();
 
    final Throwable thrown = catchThrowable(() -> MultimasterReplication.forEachDomain(domain ->
    {
      visited.add(domain);
      throw theOneError;
    }));
 
    assertThat(thrown).as("the failure thrown once the loop is over must be the one both domains threw")
        .isSameAs(theOneError);
    assertThat(thrown.getSuppressed()).as("a failure must not be suppressed under itself").isEmpty();
    assertThat(visited).as("every domain must get its turn whatever the one before it threw")
        .containsExactlyInAnyOrder(firstDomain, secondDomain);
  }
 
  @Test
  public void aFirstFailureWhichRecordsNoSuppressionStillGivesEveryDomainItsTurn()
  {
    final Error first = new ErrorWhichRecordsNoSuppression();
    final RuntimeException second = new RuntimeException("second");
    final List<LDAPReplicationDomain> visited = new ArrayList<>();
 
    final Throwable thrown = catchThrowable(() -> MultimasterReplication.forEachDomain(domain ->
    {
      visited.add(domain);
      if (visited.size() == 1)
      {
        throw first;
      }
      throw second;
    }));
 
    assertThat(thrown).as("the failure thrown once the loop is over must be the first one met")
        .isSameAs(first);
    assertThat(thrown.getSuppressed()).as("a failure which records no suppression must have none recorded")
        .isEmpty();
    assertThat(visited).as("every domain must get its turn whatever the one before it threw")
        .containsExactlyInAnyOrder(firstDomain, secondDomain);
  }
 
  /**
   * An error which keeps no record of the failures suppressed under it: the shape of the
   * error a JVM out of memory prepared beforehand, which was made without its constructor
   * and so keeps no list to record them in.
   */
  private static final class ErrorWhichRecordsNoSuppression extends Error
  {
    private static final long serialVersionUID = 1L;
 
    private ErrorWhichRecordsNoSuppression()
    {
      super("records no suppression", null, false, false);
    }
  }
}