aboutsummaryrefslogtreecommitdiffstats
path: root/src/main/java/org/onap/aai/datasnapshot/PartialVertexLoader.java
blob: 8ed80873f00231e26be100dd5c452c1860e92f96 (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
/**
 * ============LICENSE_START=======================================================
 * org.onap.aai
 * ================================================================================
 * Copyright © 2017-2018 AT&T Intellectual Property. 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.
 * ============LICENSE_END=========================================================
 */
package org.onap.aai.datasnapshot;

import com.google.gson.JsonObject;
import com.google.gson.JsonParser;
import java.io.BufferedReader;
import java.io.FileReader;
import java.util.HashMap;
import java.util.concurrent.Callable;
import org.apache.tinkerpop.gremlin.structure.Vertex;
import org.janusgraph.core.JanusGraph;
import org.onap.aai.exceptions.AAIException;
import org.onap.aai.logging.ErrorLogHelper;
import org.slf4j.Logger;



public class PartialVertexLoader implements Callable<HashMap<String,String>>{
	
	private Logger LOGGER;

	private JanusGraph jg;
	private String fName;
	private Long vertAddDelayMs;
	private Long failurePauseMs;
	private Long retryDelayMs;
	private int maxAllowedErrors;
		
	public PartialVertexLoader (JanusGraph graph, String fn, Long vertDelay, Long failurePause, 
			Long retryDelay, int maxErrors, Logger elfLog ){
		jg = graph;
		fName = fn;
		vertAddDelayMs = vertDelay;
		failurePauseMs = failurePause;
		retryDelayMs = retryDelay;
		maxAllowedErrors = maxErrors;
		LOGGER = elfLog;
	}
		
	public HashMap<String,String> call() throws Exception  {  
	
		// NOTE - we will be loading one node at a time so that bad nodes can be ignored instead of causing the
		//   entire load to fail.   
		//
		int entryCount = 0;
		int retryCount = 0;
		int failureCount = 0;
		int retryFailureCount = 0;
		HashMap <String,String> failedAttemptHash = new HashMap <String,String> ();
		HashMap <String,String> old2NewVtxIdHash = new HashMap <String,String> ();
	
		// Read this file into a JSON object
		JsonParser parser = new JsonParser();
		
		try( BufferedReader br = new BufferedReader(new FileReader(fName))) {
			// loop through the file lines and do PUT for each vertex or the edges depending on what the loadtype is
       		for(String line; (line = br.readLine()) != null; ) {
       			entryCount++;
       			Object ob = parser.parse(line);
       			JsonObject jObj = (JsonObject) ob;
       			// NOTE - we will need to keep track of how the newly generated vid's map
        		//    to the old ones so we can aim the edges correctly later.
        			
        		// ----  Note -- This ONLY loads the vertexId and the label for each vertex -------------
       			Thread.sleep(vertAddDelayMs); 
        				
        		String oldVtxIdStr = jObj.get("id").getAsString();
        		String vtxLabelStr = jObj.get("label").getAsString();
	        	try { 
        			Vertex tmpV = jg.addVertex(vtxLabelStr);
       				String newVtxIdStr = tmpV.id().toString();
       				old2NewVtxIdHash.put(oldVtxIdStr,  newVtxIdStr);
       			}
       			catch ( Exception e ){
       				failureCount++;
       				Thread.sleep(failurePauseMs); // Slow down if things are failing
       				LOGGER.debug(" >> addVertex FAILED for vtxId = " + oldVtxIdStr + ", label = [" 
       						+ vtxLabelStr + "].  ErrorMsg = [" + e.getMessage() + "]" );
        			failedAttemptHash.put(oldVtxIdStr, vtxLabelStr);
        			if( failureCount > maxAllowedErrors ) {
        				LOGGER.debug(" >>> Abandoning PartialVertexLoader() because " +
               					"Max Allowed Error count was exceeded for this thread. (max = " + 
               					maxAllowedErrors + ". ");
        				throw new Exception(" ERROR - Max Allowed Error count exceeded for this thread. (max = " + maxAllowedErrors + ". ");
        			}
        			else {
        				continue;
        			}
        		}
        		try { 
	       			jg.tx().commit();
	       		}
	       		catch ( Exception e ){
	       			failureCount++;
	       			Thread.sleep(failurePauseMs); // Slow down if things are failing
	       			LOGGER.debug(" -- COMMIT FAILED for Vtx ADD for vtxId = " + oldVtxIdStr + ", label = [" 
	       					+ vtxLabelStr + "].  ErrorMsg = [" +e.getMessage() + "]" );
        			failedAttemptHash.put(oldVtxIdStr, vtxLabelStr);
        			if( failureCount > maxAllowedErrors ) {
        				LOGGER.debug(">>> Abandoning PartialVertexLoader() because " +
               					"Max Allowed Error count was exceeded for this thread. (max = " + 
               					maxAllowedErrors + ". ");
        				throw new Exception(" ERROR - Max Allowed Error count exceeded for this thread. (max = " + maxAllowedErrors + ". ");
        			}
        			else {
        				continue;
        			}
	        	}
	        			
	        } // End of looping over each line
        		
        	if( br != null  ){
	        	br.close();
	        }
		}
		catch (Exception e) {
	        LOGGER.debug(" --- Failed in the main loop for Buffered-Reader item # " + entryCount +
	        		", fName = " + fName );
	        LOGGER.debug(" --- msg = " + e.getMessage() );
	        	AAIException ae = new AAIException("AAI_6128", e , "Failed in the main loop for Buffered-Reader item");
				ErrorLogHelper.logException(ae);
	            throw e;
		}	
        		
		// ---------------------------------------------------------------------------
       	// Now Re-Try any failed requests that might have Failed on the first pass.
       	// ---------------------------------------------------------------------------
       	try {
        	for (String failedVidStr : failedAttemptHash.keySet()) {
    			// Take a little nap, and retry this failed attempt
        		LOGGER.debug("DEBUG >> We will sleep for " + retryDelayMs + " and then RETRY any failed vertex ADDs. ");
    			Thread.sleep(retryDelayMs);
    			
    			retryCount++;
    			// When a vertex Add fails we store the label as the data in the failedAttemptHash.
    			String failedLabel = failedAttemptHash.get(failedVidStr);
    			LOGGER.debug("DEBUG >> RETRY << " +
    					failedVidStr + ", label = " + failedLabel );
    			try {
    				Vertex tmpV = jg.addVertex(failedLabel);
    				String newVtxIdStr = tmpV.id().toString();
    	      		old2NewVtxIdHash.put(failedVidStr, newVtxIdStr);
    			}
    			catch ( Exception e ){
    				retryFailureCount++;
    				LOGGER.debug(" -- addVertex FAILED for RETRY for vtxId = " +
    						failedVidStr + ", label = [" + failedLabel + 
    						"].  ErrorMsg = [" +e.getMessage() + "]" );
    				AAIException ae = new AAIException("AAI_6128", e , "addVertex FAILED for RETRY");
    				ErrorLogHelper.logException(ae);
    	            if( retryFailureCount > maxAllowedErrors ) {
        				LOGGER.debug(">>> Abandoning PartialVertexLoader() because " +
               					"Max Allowed Error count was exceeded for this thread. (max = " + 
               					maxAllowedErrors + ". ");
        				throw new Exception(" ERROR - Max Allowed Error count exceeded for this thread. (max = " + maxAllowedErrors + ". ");
        			}
        			else {
        				continue;
        			}
    			}
    			try { 
    				jg.tx().commit();
	       			LOGGER.debug(" -- addVertex Successful RETRY for vtxId = " +
    						failedVidStr + ", label = [" + failedLabel + "]");
    	       	}
    			catch ( Exception e ){
    				retryFailureCount++;
    				// Note - this is a "POSSIBLE" error because the reason the commit fails may be that
    				//    the node is a dupe or has some other legit reason that it should not be in the DB.
    				LOGGER.debug(" --POSSIBLE ERROR-- COMMIT FAILED for RETRY for vtxId = " + failedVidStr 
    						+ ", label = [" + failedLabel + "].  ErrorMsg = [" + e.getMessage() 
    						+ "].  This vertex will not be tried again. ");

    				AAIException ae = new AAIException("AAI_6128", e , "--POSSIBLE ERROR-- COMMIT FAILED for RETRY");
    				ErrorLogHelper.logException(ae);
        			if( retryFailureCount > maxAllowedErrors ) {
        				LOGGER.debug(">>> Abandoning PartialVertexLoader() because " +
               					"Max Allowed Error count was exceeded for this thread. (max = " + 
               					maxAllowedErrors + ". ");
        				throw new Exception(" ERROR - Max Allowed Error count exceeded for this thread. (max = " + maxAllowedErrors + ". ");
        			}
        			else {
        				continue;
        			}
   				}
    		} // End of looping over failed attempt hash and doing retries	
        		
		}		
        catch ( Exception e ){
			LOGGER.debug(" -- error in RETRY block. ErrorMsg = [" +e.getMessage() + "]" );
			AAIException ae = new AAIException("AAI_6128", e , " -- error in RETRY block.");
			ErrorLogHelper.logException(ae);
			throw e;	
        }
			
        // This would need to be properly logged...		
       	LOGGER.debug(">>> After Processing in PartialVertexLoader():  " + 
				entryCount + " records processed.  " + failureCount + " records failed. " +
				retryCount + " RETRYs processed.  " + retryFailureCount + " RETRYs failed. ");
        		
        return old2NewVtxIdHash;
	        
	}// end of call()  
	
	
		
}