aboutsummaryrefslogtreecommitdiffstats
path: root/docproc/src/test/java/com/yahoo/docproc/jdisc/DocumentProcessingHandlerForkTestCase.java
blob: 8396eeb597bf64bf621a047c8ea26d6fddfb57f1 (plain) (blame)
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
// Copyright Vespa.ai. Licensed under the terms of the Apache 2.0 license. See LICENSE in the project root.
package com.yahoo.docproc.jdisc;

import com.yahoo.collections.Pair;
import com.yahoo.docproc.CallStack;
import com.yahoo.docproc.DocumentProcessor;
import com.yahoo.docproc.Processing;
import com.yahoo.document.DataType;
import com.yahoo.document.Document;
import com.yahoo.document.DocumentId;
import com.yahoo.document.DocumentOperation;
import com.yahoo.document.DocumentPut;
import com.yahoo.document.DocumentType;
import com.yahoo.document.Field;
import com.yahoo.document.datatypes.StringFieldValue;
import com.yahoo.documentapi.messagebus.protocol.DocumentMessage;
import com.yahoo.documentapi.messagebus.protocol.PutDocumentMessage;
import com.yahoo.documentapi.messagebus.protocol.WriteDocumentReply;
import com.yahoo.messagebus.Message;
import com.yahoo.messagebus.Reply;
import com.yahoo.messagebus.Trace;
import org.junit.Test;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.concurrent.TimeUnit;

import static org.junit.Assert.assertArrayEquals;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertFalse;
import static org.junit.Assert.assertNotNull;
import static org.junit.Assert.assertNull;
import static org.junit.Assert.assertTrue;

/**
 * @author <a href="mailto:einarmr@yahoo-inc.com">Einar M R Rosenvinge</a>
 */
public class DocumentProcessingHandlerForkTestCase extends DocumentProcessingHandlerTestBase {

    private static final String TOMANYALLINSAMEBUCKET = "tomanyallinsamebucket";
    private static final String TOMANYSOMEINSAMEBUCKET = "tomanysomeinsamebucket";
    private static final String TOMANY = "many";
    private static final String TOONE = "toone";
    private static final String TOZERO = "tozero";
    private static final String TOMULTIPLY = "multiply";
    private final DocumentType type;

    public DocumentProcessingHandlerForkTestCase() {
        this.type = new DocumentType("baz");
        this.type.addField(new Field("blahblah", DataType.STRING));
    }

    @Override
    public DocumentType getType() {
        return type;
    }

    @Test
    public void testMessages() throws InterruptedException {
        putToFourPuts();
        putToManyAllInSameBucket();
        putToManySomeInSameBucket();
        putToOne();
        putToZero();
    }

    private Trace processDocument(String chain, String id, int numAcks) throws InterruptedException {
        assertTrue(sendMessage(chain, createPutDocumentMessage(id)));
        for (int i = 0; i < numAcks; i++) {
            Message remoteMsg = remoteServer.awaitMessage(60, TimeUnit.SECONDS);
            assertTrue(remoteMsg instanceof PutDocumentMessage);
            remoteMsg.getTrace().trace(1, "remoteServer.ack(" + id + ")");
            remoteServer.ackMessage(remoteMsg);
        }
        Reply reply = driver.client().awaitReply(60, TimeUnit.SECONDS);
        assertNotNull(reply);
        assertFalse(reply.hasErrors());
        return reply.getTrace();
    }

    private int countMatches(String text, String pattern) {
        int count = 0;
        for (int fromIndex = text.indexOf(pattern); fromIndex >= 0; fromIndex = text.indexOf(pattern, fromIndex+1)) {
            count++;
        }
        return count;
    }
    private int countSequencerMessages(String text) {
        return countMatches(text, "Sequencer sending message with sequence id");
    }
    boolean containsSequenceId(String trace, long sequence) {
        return trace.contains("Sequencer sending message with sequence id '" + sequence + "'.");
    }
    private void testNoExtraSequencingOfNormalOp() throws InterruptedException {
        String trace = processDocument(TOMULTIPLY, "id:ns:baz::noop", 1).toString();
        assertTrue(containsSequenceId(trace, 553061876));
        assertEquals(1, countSequencerMessages(trace));
    }
    private void testSequencingWhenChangingId() throws InterruptedException {
        String trace = processDocument(TOMULTIPLY, "id:ns:baz::transform", 1).toString();
        assertTrue(containsSequenceId(trace, 2033581295));
        assertTrue(containsSequenceId(trace, -633118987));
        assertEquals(2, countSequencerMessages(trace));
    }
    private void testSequencingWhenAddingOtherId() throws InterruptedException {
        String trace = processDocument(TOMULTIPLY, "id:ns:baz::append", 2).toString();
        assertTrue(containsSequenceId(trace, -334982203));
        assertTrue(containsSequenceId(trace, -633118987));
        assertEquals(2, countSequencerMessages(trace));
    }
    private void testSequencingWhenAddingSameId() throws InterruptedException {
        String trace = processDocument(TOMULTIPLY, "id:ns:baz::append_same", 2).toString();
        assertTrue(containsSequenceId(trace, 1601327001));
        assertEquals(3, countSequencerMessages(trace));
        assertEquals(3, countMatches(trace, "Sequencer sending message with sequence id '1601327001'."));
    }

    @Test
    public void testSequencing() throws InterruptedException {
        testNoExtraSequencingOfNormalOp();
        testSequencingWhenChangingId();
        testSequencingWhenAddingOtherId();
        testSequencingWhenAddingSameId();
    }

    private void putToManyAllInSameBucket() throws InterruptedException {
        assertPutMessages(createPutDocumentMessage(), TOMANYALLINSAMEBUCKET,
                          "id:123456:baz:n=11111:foo:er:bra",
                          "id:123456:baz:n=11111:foo:trallala",
                          "id:123456:baz:n=11111:foo:a");
    }

    private void putToManySomeInSameBucket() throws InterruptedException {
        assertPutMessages(createPutDocumentMessage(), TOMANYSOMEINSAMEBUCKET,
                          "id:123456:baz:n=7890:bar:er:bra",
                          "id:foo:baz::er:ja",
                          "id:567890:baz:n=1234:a",
                          "id:foo:baz::hahahhaa",
                          "id:123456:baz:n=7890:a:a",
                          "id:foo:baz::aa",
                          "id:567890:baz:n=1234:bar:ala",
                          "id:foo:baz::sdfgsaa",
                          "id:123456:baz:n=7890:bar:tralsfa",
                          "id:foo:baz::dfshaa");
    }

    private void putToFourPuts() throws InterruptedException {
        assertPutMessages(createPutDocumentMessage(), TOMANY,
                          "id:foo:baz::er:bra",
                          "id:foo:baz::er:ja",
                          "id:foo:baz::hahahhaa",
                          "id:foo:baz::trallala");
    }

    private void putToOne() throws InterruptedException {
        assertPutMessages(createPutDocumentMessage(), TOONE,
                          "id:ns:baz::bar");
    }

    private void putToZero() throws InterruptedException {
        assertTrue(sendMessage(TOZERO, createPutDocumentMessage()));

        Reply reply = driver.client().awaitReply(60, TimeUnit.SECONDS);
        assertTrue(reply instanceof WriteDocumentReply);
        assertFalse(reply.hasErrors());
    }

    @Override
    protected List<Pair<String, CallStack>> getCallStacks() {
        ArrayList<Pair<String, CallStack>> stacks = new ArrayList<>(6);
        stacks.add(new Pair<>(TOMANYALLINSAMEBUCKET, new CallStack().addLast(new OneToManyDocumentsAllInSameBucketProcessor())));
        stacks.add(new Pair<>(TOMANYSOMEINSAMEBUCKET, new CallStack().addLast(new OneToManyDocumentsSomeInSameBucketProcessor())));
        stacks.add(new Pair<>(TOMANY, new CallStack().addLast(new OneToManyDocumentsProcessor())));
        stacks.add(new Pair<>(TOONE, new CallStack().addLast(new OneToOneDocumentsProcessor())));
        stacks.add(new Pair<>(TOZERO, new CallStack().addLast(new OneToZeroDocumentsProcessor())));
        stacks.add(new Pair<>(TOMULTIPLY, new CallStack().addLast(new MultiplyProcessor())));
        return stacks;
    }

    protected PutDocumentMessage createPutDocumentMessage() {
        return createPutDocumentMessage("id:ns:baz::bar");
    }
    protected PutDocumentMessage createPutDocumentMessage(String id) {
        Document document = new Document(getType(), id);
        document.setFieldValue("blahblah", new StringFieldValue("This is a test."));
        return new PutDocumentMessage(new DocumentPut(document));
    }

    private void assertPutMessages(DocumentMessage msg, String route, String... expected) throws InterruptedException {
        msg.getTrace().setLevel(9);
        assertTrue(sendMessage(route, msg));

        String[] actual = new String[expected.length];
        for (int i = 0; i < expected.length; ++i) {
            Message remoteMsg = remoteServer.awaitMessage(60, TimeUnit.SECONDS);
            assertTrue(remoteMsg instanceof PutDocumentMessage);
            remoteMsg.getTrace().trace(1, "remoteServer.ack(" + expected[i] + ")");
            remoteServer.ackMessage(remoteMsg);
            actual[i] = ((PutDocumentMessage)remoteMsg).getDocumentPut().getDocument().getId().toString();
        }
        assertNull(remoteServer.awaitMessage(100, TimeUnit.MILLISECONDS));

        Arrays.sort(expected);
        Arrays.sort(actual);
        assertArrayEquals(expected, actual);

        Reply reply = driver.client().awaitReply(60, TimeUnit.SECONDS);
        assertNotNull(reply);
        assertFalse(reply.hasErrors());
        String trace = reply.getTrace().toString();
        for (String documentId : expected) {
            assertTrue("missing trace for document '" + documentId + "'\n" + trace,
                       trace.contains("remoteServer.ack(" + documentId + ")"));
        }
        if (expected.length == 1) {
            assertFalse("unexpected fork in trace for single document\n" + trace,
                        trace.contains("<fork>"));
        } else {
            assertTrue("missing fork in trace for " + expected.length + " split\n" + trace,
                       trace.contains("<fork>"));
        }
    }

    public static class OneToOneDocumentsProcessor extends DocumentProcessor {

        @Override
        public Progress process(Processing processing) {
            return Progress.DONE;
        }
    }

    public class OneToManyDocumentsProcessor extends DocumentProcessor {

        @Override
        public Progress process(Processing processing) {
            List<DocumentOperation> operations = processing.getDocumentOperations();
            operations.clear();
            operations.add(new DocumentPut(type, "id:foo:baz::er:bra"));
            operations.add(new DocumentPut(type, "id:foo:baz::er:ja"));
            operations.add(new DocumentPut(type, "id:foo:baz::trallala"));
            operations.add(new DocumentPut(type, "id:foo:baz::hahahhaa"));
            return Progress.DONE;
        }
    }

    public static class OneToZeroDocumentsProcessor extends DocumentProcessor {

        @Override
        public Progress process(Processing processing) {
            processing.getDocumentOperations().clear();
            return Progress.DONE;
        }
    }

    public class OneToManyDocumentsSomeInSameBucketProcessor extends DocumentProcessor {

        @Override
        public Progress process(Processing processing) {
            List<DocumentOperation> operations = processing.getDocumentOperations();
            operations.clear();
            operations.add(new DocumentPut(type, "id:123456:baz:n=7890:bar:er:bra"));
            operations.add(new DocumentPut(type, "id:foo:baz::er:ja"));
            operations.add(new DocumentPut(type, "id:567890:baz:n=1234:a"));
            operations.add(new DocumentPut(type, "id:foo:baz::hahahhaa"));
            operations.add(new DocumentPut(type, "id:123456:baz:n=7890:a:a"));
            operations.add(new DocumentPut(type, "id:foo:baz::aa"));
            operations.add(new DocumentPut(type, "id:567890:baz:n=1234:bar:ala"));
            operations.add(new DocumentPut(type, "id:foo:baz::sdfgsaa"));
            operations.add(new DocumentPut(type, "id:123456:baz:n=7890:bar:tralsfa"));
            operations.add(new DocumentPut(type, "id:foo:baz::dfshaa"));
            return Progress.DONE;
        }

    }

    public class OneToManyDocumentsAllInSameBucketProcessor extends DocumentProcessor {

        @Override
        public Progress process(Processing processing) {
            List<DocumentOperation> docs = processing.getDocumentOperations();
            docs.clear();
            docs.add(new DocumentPut(type, "id:123456:baz:n=11111:foo:er:bra"));
            docs.add(new DocumentPut(type, "id:123456:baz:n=11111:foo:trallala"));
            docs.add(new DocumentPut(type, "id:123456:baz:n=11111:foo:a"));
            return Progress.DONE;
        }

    }

    public class MultiplyProcessor extends DocumentProcessor {

        @Override
        public Progress process(Processing processing) {
            List<DocumentOperation> docs = processing.getDocumentOperations();
            assertEquals(1, docs.size());
            DocumentId id = docs.get(0).getId();
            if ("id:ns:baz::transform".equals(id.toString())) {
                docs.clear();
                docs.add(new DocumentPut(type, "id:ns:baz::appended"));
            } else if ("id:ns:baz::append".equals(id.toString())) {
                docs.add(new DocumentPut(type, "id:ns:baz::appended"));
            } else if ("id:ns:baz::append_same".equals(id.toString())) {
                docs.add(new DocumentPut(type, "id:ns:baz::append_same"));

            }
            return Progress.DONE;
        }

    }

}