aboutsummaryrefslogtreecommitdiffhomepage
path: root/src/test/java/com/google/devtools/build/lib/actions/cache/PersistentStringIndexerTest.java
blob: 7fc0cb1327425994e0e3587f470b9ddcd1a399f8 (plain)
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
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
// Copyright 2015 Google Inc. All rights reserved.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
//    http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
package com.google.devtools.build.lib.actions.cache;

import static com.google.common.truth.Truth.assertThat;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertFalse;
import static org.junit.Assert.assertTrue;
import static org.junit.Assert.fail;

import com.google.devtools.build.lib.testutil.TestThread;
import com.google.devtools.build.lib.util.Clock;
import com.google.devtools.build.lib.vfs.FileSystemUtils;
import com.google.devtools.build.lib.vfs.Path;
import com.google.devtools.build.lib.vfs.util.FsApparatus;

import org.junit.Before;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.junit.runners.JUnit4;

import java.io.EOFException;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collection;
import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;
import java.util.concurrent.CountDownLatch;

/**
 * Test for the PersistentStringIndexer class.
 */
@RunWith(JUnit4.class)
public class PersistentStringIndexerTest {

  private static class ManualClock implements Clock {
    private long currentTime = 0L;

    ManualClock() { }

    @Override public long currentTimeMillis() {
      throw new AssertionError("unexpected method call");
    }

    @Override  public long nanoTime() {
      return currentTime;
    }

    void advance(long time) {
      currentTime += time;
    }
  }

  private PersistentStringIndexer psi;
  private Map<Integer, String> mappings = new ConcurrentHashMap<>();
  private FsApparatus scratch = FsApparatus.newInMemory();
  private ManualClock clock = new ManualClock();
  private Path dataPath;
  private Path journalPath;


  @Before
  public void setUp() throws Exception {
    dataPath = scratch.path("/cache/test.dat");
    journalPath = scratch.path("/cache/test.journal");
    psi = PersistentStringIndexer.newPersistentStringIndexer(dataPath, clock);
  }

  private void assertSize(int expected) {
    assertEquals(expected, psi.size());
  }

  private void assertIndex(int expected, String s) {
    int index = psi.getOrCreateIndex(s);
    assertEquals(expected, index);
    mappings.put(expected, s);
  }

  private void assertContent() {
    for (int i = 0; i < psi.size(); i++) {
      if(mappings.get(i) != null) {
        assertEquals(mappings.get(i), psi.getStringForIndex(i));
      }
    }
  }


  private void setupTestContent() {
    assertSize(0);
    assertIndex(0, "abcdefghi");  // Create leafs
    assertIndex(1, "abcdefjkl");
    assertIndex(2, "abcdefmno");
    assertIndex(3, "abcdefjklpr");
    assertIndex(3, "abcdefjklpr");
    assertIndex(4, "abcdstr");
    assertIndex(5, "012345");
    assertSize(6);
    assertIndex(6, "abcdef");  // Validate inner nodes
    assertIndex(7, "abcd");
    assertIndex(8, "");
    assertSize(9);
    assertContent();
  }

  /**
   * Writes lots of entries with labels "fooconcurrent[int]" at the same time.
   * The set of labels written is deterministic, but the label:index mapping is
   * not.
   */
  private void writeLotsOfEntriesConcurrently(final int numToWrite) throws InterruptedException {
    final int NUM_THREADS = 10;
    final CountDownLatch synchronizerLatch = new CountDownLatch(NUM_THREADS);

    class IndexAdder extends TestThread {
      @Override
      public void runTest() throws Exception {
        for (int i = 0; i < numToWrite; i++) {
          synchronizerLatch.countDown();
          synchronizerLatch.await();

          String value = "fooconcurrent" + i;
          mappings.put(psi.getOrCreateIndex(value), value);
        }
      }
    }

    Collection<TestThread> threads = new ArrayList<>();
    for (int i = 0; i < NUM_THREADS; i++) {
      TestThread thread = new IndexAdder();
      thread.start();
      threads.add(thread);
    }

    for (TestThread thread : threads) {
      thread.joinAndAssertState(0);
    }
  }

  @Test
  public void testNormalOperation() throws Exception {
    assertFalse(dataPath.exists());
    assertFalse(journalPath.exists());
    setupTestContent();
    assertFalse(dataPath.exists());
    assertFalse(journalPath.exists());

    clock.advance(4);
    assertIndex(9, "xyzqwerty"); // This should flush journal to disk.
    assertFalse(dataPath.exists());
    assertTrue(journalPath.exists());

    psi.save(); // Successful save will remove journal file.
    assertTrue(dataPath.exists());
    assertFalse(journalPath.exists());

    // Now restore data from file and verify it.
    psi = PersistentStringIndexer.newPersistentStringIndexer(dataPath, clock);
    assertFalse(journalPath.exists());
    clock.advance(4);
    assertSize(10);
    assertContent();
    assertFalse(journalPath.exists());
  }

  @Test
  public void testJournalRecoveryWithoutMainDataFile() throws Exception {
    assertFalse(dataPath.exists());
    assertFalse(journalPath.exists());
    setupTestContent();
    assertFalse(dataPath.exists());
    assertFalse(journalPath.exists());

    clock.advance(4);
    assertIndex(9, "abc1234"); // This should flush journal to disk.
    assertFalse(dataPath.exists());
    assertTrue(journalPath.exists());

    // Now restore data from file and verify it. All data should be restored from journal;
    psi = PersistentStringIndexer.newPersistentStringIndexer(dataPath, clock);
    assertTrue(dataPath.exists());
    assertFalse(journalPath.exists());
    clock.advance(4);
    assertSize(10);
    assertContent();
    assertFalse(journalPath.exists());
  }

  @Test
  public void testJournalRecovery() throws Exception {
    assertFalse(dataPath.exists());
    assertFalse(journalPath.exists());
    setupTestContent();
    psi.save();
    assertTrue(dataPath.exists());
    assertFalse(journalPath.exists());
    long oldDataFileLen = dataPath.getFileSize();

    clock.advance(4);
    assertIndex(9, "another record"); // This should flush journal to disk.
    assertSize(10);
    assertTrue(dataPath.exists());
    assertTrue(journalPath.exists());

    // Now restore data from file and verify it. All data should be restored from journal;
    psi = PersistentStringIndexer.newPersistentStringIndexer(dataPath, clock);
    assertTrue(dataPath.exists());
    assertFalse(journalPath.exists());
    assertTrue(dataPath.getFileSize() > oldDataFileLen); // data file should have been updated
    clock.advance(4);
    assertSize(10);
    assertContent();
    assertFalse(journalPath.exists());
  }

  @Test
  public void testConcurrentWritesJournalRecovery() throws Exception {
    assertFalse(dataPath.exists());
    assertFalse(journalPath.exists());
    setupTestContent();
    psi.save();
    assertTrue(dataPath.exists());
    assertFalse(journalPath.exists());
    long oldDataFileLen = dataPath.getFileSize();

    int size = psi.size();
    int numToWrite = 50000;
    writeLotsOfEntriesConcurrently(numToWrite);
    assertFalse(journalPath.exists());
    clock.advance(4);
    assertIndex(size + numToWrite, "another record"); // This should flush journal to disk.
    assertSize(size + numToWrite + 1);
    assertTrue(dataPath.exists());
    assertTrue(journalPath.exists());

    // Now restore data from file and verify it. All data should be restored from journal;
    psi = PersistentStringIndexer.newPersistentStringIndexer(dataPath, clock);
    assertTrue(dataPath.exists());
    assertFalse(journalPath.exists());
    assertTrue(dataPath.getFileSize() > oldDataFileLen); // data file should have been updated
    clock.advance(4);
    assertSize(size + numToWrite + 1);
    assertContent();
    assertFalse(journalPath.exists());
  }

  @Test
  public void testCorruptedJournal() throws Exception {
    FileSystemUtils.createDirectoryAndParents(journalPath.getParentDirectory());
    FileSystemUtils.writeContentAsLatin1(journalPath, "bogus content");
    try {
      psi = PersistentStringIndexer.newPersistentStringIndexer(dataPath, clock);
      fail();
    } catch (IOException e) {
      assertThat(e.getMessage()).contains("too short: Only 13 bytes");
    }

    journalPath.delete();
    setupTestContent();
    assertFalse(dataPath.exists());
    assertFalse(journalPath.exists());

    clock.advance(4);
    assertIndex(9, "abc1234"); // This should flush journal to disk.
    assertFalse(dataPath.exists());
    assertTrue(journalPath.exists());

    byte[] journalContent = FileSystemUtils.readContent(journalPath);

    // Now restore data from file and verify it. All data should be restored from journal;
    psi = PersistentStringIndexer.newPersistentStringIndexer(dataPath, clock);
    assertTrue(dataPath.exists());
    assertFalse(journalPath.exists());

    // Now put back truncated journal. We should get an error.
    assertTrue(dataPath.delete());
    FileSystemUtils.writeContent(journalPath,
        Arrays.copyOf(journalContent, journalContent.length - 1));
    try {
      psi = PersistentStringIndexer.newPersistentStringIndexer(dataPath, clock);
      fail();
    } catch (EOFException e) {
      // Expected.
    }

    // Corrupt the journal with a negative size value.
    byte[] journalCopy = Arrays.copyOf(journalContent, journalContent.length);
    // Flip this bit to make the key size negative.
    journalCopy[95] = -2;
    FileSystemUtils.writeContent(journalPath,  journalCopy);
    try {
      psi = PersistentStringIndexer.newPersistentStringIndexer(dataPath, clock);
      fail();
    } catch (IOException e) {
      // Expected.
      assertThat(e.getMessage()).contains("corrupt key length");
    }

    // Now put back corrupted journal. We should get an error.
    journalContent[journalContent.length - 13] = 100;
    FileSystemUtils.writeContent(journalPath,  journalContent);
    try {
      psi = PersistentStringIndexer.newPersistentStringIndexer(dataPath, clock);
      fail();
    } catch (IOException e) {
      // Expected.
    }
  }

  @Test
  public void testDupeIndexCorruption() throws Exception {
    setupTestContent();
    assertFalse(dataPath.exists());
    assertFalse(journalPath.exists());

    assertIndex(9, "abc1234"); // This should flush journal to disk.
    psi.save();
    assertTrue(dataPath.exists());
    assertFalse(journalPath.exists());

    byte[] content = FileSystemUtils.readContent(dataPath);

    // We remove the data file, and instead create a corrupt journal.
    //
    // The journal has a header followed by a sequence of (String, int) pairs, where each int is a
    // unique value. The String is encoded by the length (as an int), and the int is simply encoded
    // as an int. Note that the DataOutputStream class uses big endian by default, so the low-order
    // bits are at the end.
    //
    // For the purpose of this test, we want to make the journal contain two entries with the same
    // index (which is illegal). The PersistentStringIndexer assigns int values in the usual order,
    // starting with zero, and it now contains 9 entries. We simply change the last entry to an
    // index that is guaranteed to already exist. If it is the index 1, we change it to 2, otherwise
    // we change it to 1 - in both cases, the code currently guarantees that the duplicate comes
    // earlier in the stream.
    assertTrue(dataPath.delete());
    content[content.length - 1] = content[content.length - 1] == 1 ? (byte) 2 : (byte) 1;
    FileSystemUtils.writeContent(journalPath, content);

    try {
      psi = PersistentStringIndexer.newPersistentStringIndexer(dataPath, clock);
      fail();
    } catch (IOException e) {
      // Expected.
      assertThat(e.getMessage()).contains("Corrupted filename index has duplicate entry");
    }
  }

  @Test
  public void testDeferredIOFailure() throws Exception {
    assertFalse(dataPath.exists());
    assertFalse(journalPath.exists());
    setupTestContent();
    assertFalse(dataPath.exists());
    assertFalse(journalPath.exists());

    // Ensure that journal cannot be saved.
    FileSystemUtils.createDirectoryAndParents(journalPath);

    clock.advance(4);
    assertIndex(9, "abc1234"); // This should flush journal to disk (and fail at that).
    assertFalse(dataPath.exists());

    // Subsequent updates should succeed even though journaling is disabled at this point.
    clock.advance(4);
    assertIndex(10, "another record");
    try {
      // Save should actually save main data file but then return us deferred IO failure
      // from failed journal write.
      psi.save();
      fail();
    } catch(IOException e) {
      assertThat(e.getMessage()).contains(journalPath.getPathString() + " (Is a directory)");
    }
  }
}