aboutsummaryrefslogtreecommitdiffstats
path: root/vespa-hadoop/src/main/java/com/yahoo/vespa/hadoop/mapreduce/VespaRecordWriter.java
blob: 600658067cbc02ee20c24bbf606695447f393291 (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
// Copyright 2017 Yahoo Holdings. Licensed under the terms of the Apache 2.0 license. See LICENSE in the project root.
package com.yahoo.vespa.hadoop.mapreduce;

import com.fasterxml.jackson.core.JsonFactory;
import com.fasterxml.jackson.core.JsonParseException;
import com.fasterxml.jackson.core.JsonParser;
import com.fasterxml.jackson.core.JsonToken;
import com.yahoo.vespa.hadoop.mapreduce.util.VespaConfiguration;
import com.yahoo.vespa.hadoop.mapreduce.util.VespaCounters;
import com.yahoo.vespa.hadoop.pig.VespaDocumentOperation;
import com.yahoo.vespa.http.client.FeedClient;
import com.yahoo.vespa.http.client.FeedClientFactory;
import com.yahoo.vespa.http.client.Result;
import com.yahoo.vespa.http.client.config.Cluster;
import com.yahoo.vespa.http.client.config.ConnectionParams;
import com.yahoo.vespa.http.client.config.Endpoint;
import com.yahoo.vespa.http.client.config.FeedParams;
import com.yahoo.vespa.http.client.config.FeedParams.DataFormat;
import com.yahoo.vespa.http.client.config.SessionParams;
import org.apache.hadoop.mapreduce.RecordWriter;
import org.apache.hadoop.mapreduce.TaskAttemptContext;

import javax.xml.namespace.QName;
import javax.xml.stream.FactoryConfigurationError;
import javax.xml.stream.XMLEventReader;
import javax.xml.stream.XMLInputFactory;
import javax.xml.stream.XMLStreamException;
import javax.xml.stream.events.StartElement;
import javax.xml.stream.events.XMLEvent;
import java.io.IOException;
import java.io.StringReader;
import java.util.List;
import java.util.StringTokenizer;
import java.util.concurrent.ThreadLocalRandom;
import java.util.logging.Logger;

/**
 * VespaRecordWriter sends the output <key, value> to one or more Vespa
 * endpoints.
 *
 * @author lesters
 */
@SuppressWarnings("rawtypes")
public class VespaRecordWriter extends RecordWriter {

    private final static Logger log = Logger.getLogger(VespaRecordWriter.class.getCanonicalName());

    private boolean initialized = false;
    private FeedClient feedClient;
    private final VespaCounters counters;
    private final int progressInterval;

    final VespaConfiguration configuration;

    VespaRecordWriter(VespaConfiguration configuration, VespaCounters counters) {
        this.counters = counters;
        this.configuration = configuration;
        this.progressInterval = configuration.progressInterval();
    }


    @Override
    public void write(Object key, Object data) throws IOException, InterruptedException {
        if (!initialized) {
            initialize();
        }

        String doc = data.toString().trim();

        // Parse data to find document id - if none found, skip this write
        String docId = DataFormat.JSON_UTF8.equals(configuration.dataFormat()) ? findDocId(doc)
                : findDocIdFromXml(doc);
        if (docId != null && docId.length() >= 0) {
            feedClient.stream(docId, doc);
            counters.incrementDocumentsSent(1);
        } else {
            counters.incrementDocumentsSkipped(1);
        }

        if (counters.getDocumentsSent() % progressInterval == 0) {
            String progress = String.format("Feed progress: %d / %d / %d / %d (sent, ok, failed, skipped)",
                    counters.getDocumentsSent(),
                    counters.getDocumentsOk(),
                    counters.getDocumentsFailed(),
                    counters.getDocumentsSkipped());
            log.info(progress);
        }

    }


    @Override
    public void close(TaskAttemptContext taskAttemptContext) throws IOException, InterruptedException {
        if (feedClient != null) {
            feedClient.close();
        }
    }

    protected ConnectionParams.Builder configureConnectionParams() {
        ConnectionParams.Builder connParamsBuilder = new ConnectionParams.Builder();
        connParamsBuilder.setDryRun(configuration.dryrun());
        connParamsBuilder.setUseCompression(configuration.useCompression());
        connParamsBuilder.setEnableV3Protocol(configuration.useV3Protocol());
        connParamsBuilder.setNumPersistentConnectionsPerEndpoint(configuration.numConnections());
        connParamsBuilder.setMaxRetries(configuration.numRetries());
        if (configuration.proxyHost() != null) {
            connParamsBuilder.setProxyHost(configuration.proxyHost());
        }
        if (configuration.proxyPort() >= 0) {
            connParamsBuilder.setProxyPort(configuration.proxyPort());
        }
        return connParamsBuilder;
    }

    protected FeedParams.Builder configureFeedParams() {
        FeedParams.Builder feedParamsBuilder = new FeedParams.Builder();
        feedParamsBuilder.setDataFormat(configuration.dataFormat());
        feedParamsBuilder.setRoute(configuration.route());
        feedParamsBuilder.setMaxSleepTimeMs(configuration.maxSleepTimeMs());
        feedParamsBuilder.setMaxInFlightRequests(configuration.maxInFlightRequests());
        feedParamsBuilder.setLocalQueueTimeOut(3600*1000); //1 hour queue timeout
        return feedParamsBuilder;
    }

    protected SessionParams.Builder configureSessionParams() {
        SessionParams.Builder sessionParamsBuilder = new SessionParams.Builder();
        sessionParamsBuilder.setThrottlerMinSize(configuration.throttlerMinSize());
        sessionParamsBuilder.setClientQueueSize(configuration.maxInFlightRequests()*2);
        return sessionParamsBuilder;
    }
    
    private void initialize() {
        if (!configuration.dryrun() && configuration.randomStartupSleepMs() > 0) {
            int delay = ThreadLocalRandom.current().nextInt(configuration.randomStartupSleepMs());
            log.info("VespaStorage: Delaying startup by " + delay + " ms");
            try {
                Thread.sleep(delay);
            } catch (Exception e) {}
        }

        ConnectionParams.Builder connParamsBuilder = configureConnectionParams();
        FeedParams.Builder feedParamsBuilder = configureFeedParams();
        SessionParams.Builder sessionParams = configureSessionParams();

        sessionParams.setConnectionParams(connParamsBuilder.build());
        sessionParams.setFeedParams(feedParamsBuilder.build());

        String endpoints = configuration.endpoint();
        StringTokenizer tokenizer = new StringTokenizer(endpoints, ",");
        while (tokenizer.hasMoreTokens()) {
            String endpoint = tokenizer.nextToken().trim();
            sessionParams.addCluster(new Cluster.Builder().addEndpoint(
                    Endpoint.create(endpoint, configuration.defaultPort(), configuration.useSSL())
            ).build());
        }

        ResultCallback resultCallback = new ResultCallback(counters);
        feedClient = FeedClientFactory.create(sessionParams.build(), resultCallback);

        initialized = true;
        log.info("VespaStorage configuration:\n" + configuration.toString());
        log.info(feedClient.getStatsAsJson());
    }

    private String findDocIdFromXml(String xml) {
        try {
            XMLEventReader eventReader = XMLInputFactory.newInstance().createXMLEventReader(new StringReader(xml));
            while (eventReader.hasNext()) {
                XMLEvent event = eventReader.nextEvent();
                if (event.getEventType() == XMLEvent.START_ELEMENT) {
                    StartElement element = event.asStartElement();
                    String elementName = element.getName().getLocalPart();
                    if (VespaDocumentOperation.Operation.valid(elementName)) {
                        return element.getAttributeByName(QName.valueOf("documentid")).getValue();
                    }
                }
            }
        } catch (XMLStreamException | FactoryConfigurationError e) {
            // as json dude does
            return null;
        }
        return null;
    }
    
    private String findDocId(String json) throws IOException {
        JsonFactory factory = new JsonFactory();
        try(JsonParser parser = factory.createParser(json)) {
            if (parser.nextToken() != JsonToken.START_OBJECT) {
                return null;
            }
            while (parser.nextToken() != JsonToken.END_OBJECT) {
                String fieldName = parser.getCurrentName();
                parser.nextToken();
                if (VespaDocumentOperation.Operation.valid(fieldName)) {
                    String docId = parser.getText();
                    return docId;
                } else {
                    parser.skipChildren();
                }
            }
        } catch (JsonParseException ex) {
            return null;
        }
        return null;
    }


    static class ResultCallback implements FeedClient.ResultCallback {
        final VespaCounters counters;

        public ResultCallback(VespaCounters counters) {
            this.counters = counters;
        }

        @Override
        public void onCompletion(String docId, Result documentResult) {
            if (!documentResult.isSuccess()) {
                counters.incrementDocumentsFailed(1);
                StringBuilder sb = new StringBuilder();
                sb.append("Problems with docid ");
                sb.append(docId);
                sb.append(": ");
                List<Result.Detail> details = documentResult.getDetails();
                for (Result.Detail detail : details) {
                    sb.append(detail.toString());
                    sb.append(" ");
                }
                log.warning(sb.toString());
                return;
            }
            counters.incrementDocumentsOk(1);
        }

    }

}