Project

General

Profile

1
/**
2
 *  '$RCSfile$'
3
 *    Purpose: A class to asyncronously force the replication of each server
4
 *             that has an entry in the xml_replication table.  When run,
5
 *             this thread communicates with each server in the list and
6
 *             solicites a read of an updated or newly inserted document
7
 *             with a certain docid.
8
 *  Copyright: 2000 Regents of the University of California and the
9
 *             National Center for Ecological Analysis and Synthesis
10
 *    Authors: Chad Berkley
11
 *
12
 *   '$Author: tao $'
13
 *     '$Date: 2007-04-12 18:13:08 -0700 (Thu, 12 Apr 2007) $'
14
 * '$Revision: 3229 $'
15
 *
16
 * This program is free software; you can redistribute it and/or modify
17
 * it under the terms of the GNU General Public License as published by
18
 * the Free Software Foundation; either version 2 of the License, or
19
 * (at your option) any later version.
20
 *
21
 * This program is distributed in the hope that it will be useful,
22
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
23
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
24
 * GNU General Public License for more details.
25
 *
26
 * You should have received a copy of the GNU General Public License
27
 * along with this program; if not, write to the Free Software
28
 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
29
 */
30

    
31
package edu.ucsb.nceas.metacat;
32

    
33
import java.util.*;
34
import java.io.*;
35
import java.sql.*;
36
import java.net.*;
37
import java.lang.*;
38
import java.text.*;
39

    
40
import org.apache.log4j.Logger;
41

    
42
/**
43
 * A class to asyncronously force the replication of each server
44
 * that has an entry in the xml_replication table.  When run,
45
 * this thread communicates with each server in the list and
46
 * solicites a read of an updated or newly inserted document
47
 * with a certain docid.
48
 */
49
public class ForceReplicationHandler implements Runnable
50
{
51
  private Thread btThread;
52
  private MetaCatUtil util = new MetaCatUtil();
53
  private String docid;
54
  private String action;
55
  private boolean xmlDocument;
56
  private boolean dbactionFlag = true;
57
  private String user = null;
58
  private String ipAddress = null;
59
  private ReplicationServerList serverLists = null;//Serverlist
60
  private int homeServerCode = 0; // home server code for the docid
61
  // When a metacat A got forcereplication
62
  // notification from B, then A will update its record. And A will notification
63
  // other metacat in its serverlist to update this docid if A is a hub. But we
64
  // don't want A to notify B again. B is nofitification of A
65
  private String notificationServer = null;
66
  private Logger logMetacat = Logger.getLogger(ForceReplicationHandler.class);
67

    
68
  //constant
69
  public static final String DELETE = "delete";
70

    
71

    
72

    
73
  /**
74
   * Constructor of ForceReplicationHandler
75
   * @param docid the docid to force replicate
76
   * @param the action that is being performed on the document (either
77
   *        INSERT or UPDATE)
78
   * @param xml the docid is a xml document or not (data file)
79
   * @param notificationServer, when a metacat A got forcereplication
80
   * notification from B, then A will update its record. And A will notification
81
   * other metacat in its serverlist to update this docid if A is a hub. But we
82
   * don't want A to notify B again. B is nofitification of A.
83
   */
84
  public ForceReplicationHandler(String docid, String action, boolean xml,
85
                                                   String myNotificationServer, String user, String ipAddress)
86
  {
87
    this.docid = docid;
88
    this.action = action;
89
    this.xmlDocument =xml;
90
    this.user = user;
91
    this.ipAddress = ipAddress;
92
    // Build a severLists from xml_replication table
93
    this.serverLists = new ReplicationServerList();
94
    // Get sever code for this docid
95
    try
96
    {
97
      this.homeServerCode = MetacatReplication.getHomeServerCodeForDocId(docid);
98
    }//try
99
    catch (Exception e)
100
    {
101
      logMetacat.error("Error in ForceReplicationHandle(): "
102
                                                          +e.getMessage());
103
    }//catch
104
    // Get the notification server
105
    this.notificationServer = myNotificationServer;
106

    
107
    if(this.action.equals(""))
108
    {
109
      dbactionFlag = false;
110
    }
111

    
112
    btThread = new Thread(this);
113
    btThread.setPriority(Thread.MIN_PRIORITY);
114
    btThread.start();
115
  }
116

    
117
  /**
118
   * Use this constructor when the action is implied.
119
   */
120
  public ForceReplicationHandler(String docid, boolean xml,
121
                                                String myNotificationServer, String user, String ipAddress)
122
  {
123
    this.docid = docid;
124
    this.xmlDocument = xml;
125
    this.user = user;
126
    this.ipAddress = ipAddress;
127
    dbactionFlag = false;
128
    // Build a severLists from xml_replication table
129
    this.serverLists = new ReplicationServerList();
130
    // Get home server code for this doicd
131
    try
132
    {
133
      this.homeServerCode = MetacatReplication.getHomeServerCodeForDocId(docid);
134
    }
135
     catch (Exception e)
136
    {
137
      logMetacat.error("Error in ForceReplicationHandle(): "
138
                                                          +e.getMessage());
139
    }//catch
140
    // Get notification server
141
    this.notificationServer = myNotificationServer;
142
    btThread = new Thread(this);
143
    btThread.setPriority(Thread.MIN_PRIORITY);
144
    btThread.start();
145
  }
146

    
147
  /**
148
   * Method to send force replication command to other server to get
149
   * a new or updated docid
150
   */
151
  public void run()
152
  {
153

    
154
    
155
      // URL for notifcation
156
      URL comeAndGetIt = null;
157
      // If no server in xml_replication table, metacat don't need do anything
158
      if (serverLists.isEmpty())
159
      {
160
        return;
161
      }
162

    
163
      // Thread seelping for some seconds to make sure the document was insert
164
      // into the database before we send force replication request
165
      int sleepTime = Integer.parseInt
166
                         (MetaCatUtil.getOption("forcereplicationwaitingtime"));
167
      try
168
      {
169
        Thread.sleep(sleepTime);
170
      }
171
      catch(Exception ee)
172
      {
173
        logMetacat.error("Couldn't sleep in force replication");
174
      }
175
      logMetacat.info("notification server:"+notificationServer);
176
      // Check every server in the serverlists
177
      for (int i=0; i<serverLists.size(); i++)
178
      {
179
        //Set comeAndGetIt null
180
        comeAndGetIt = null;
181
         // Get ReplicationServer object in index i
182
        ReplicationServer replicationServer = serverLists.serverAt(i);
183
        // Get this ReplicationServer 's name
184
        String server = replicationServer.getServerName();
185
        try
186
        {
187
        
188

    
189
        // If the server is the notification server, we don't notify it back
190
        // again, if server is null we don't replication it
191
        if (server!=null && !server.equals(notificationServer))
192
        {
193

    
194
          if(dbactionFlag)
195
          {
196
            // xml documents and server can replicate xml doucment
197
            if (xmlDocument && replicationServer.getReplication())
198
            {
199
              // If this docid's homeserver is localhost, replicate it
200
              if (homeServerCode==1)
201
              {
202
                MetacatReplication.replLog("force xml replicating to "
203
                                                                    + server);
204
                comeAndGetIt = new URL("https://" + server +
205
                                 "?action=forcereplicate&server=" +
206
                                  util.getLocalReplicationServerName()+
207
                                 "&docid=" + docid + "&dbaction=" +
208
                                  action+"&ipAddress="+ipAddress);
209
                //over write the url for delete
210
                if (action != null && action.equals(DELETE))
211
                {
212
                  comeAndGetIt = new URL("https://" + server + "?action=" +
213
                                   MetacatReplication.FORCEREPLICATEDELETE +
214
                                  "&docid=" + docid +"&server="+ util.getLocalReplicationServerName()+"&user="+user+"&ipAddress="+ipAddress);
215

    
216
                }
217
              }//if servercode==1
218
              else if (replicationServer.getHub()||
219
                   server.equals(MetacatReplication.
220
                                  getServerNameForServerCode(homeServerCode)))
221
              {
222
                // If the docid's home server is not local host, but local host
223
                // is a hub of this server, replicate the docid too.
224
                // If the server is homeserver of the docid, replication it too
225
                MetacatReplication.replLog("force xml replicating to "
226
                                                                    + server);
227
                comeAndGetIt = new URL("https://" + server +
228
                                 "?action=forcereplicate&server=" +
229
                                 util.getLocalReplicationServerName() +
230
                                 "&docid=" + docid + "&dbaction=" +
231
                                  action+"&ipAddress="+ipAddress);
232
                //over write the url for delete
233
               if (action != null && action.equals(DELETE))
234
               {
235
                  comeAndGetIt = new URL("https://" + server + "?action=" +
236
                            MetacatReplication.FORCEREPLICATEDELETE +
237
                           "&docid=" + docid+"&server="+ util.getLocalReplicationServerName()+"&user="+user+"&ipAddress="+ipAddress);
238

    
239
                }
240
              }//else
241

    
242
            }//if xmlDocument
243
            //It is data file and configured to handle data replication
244
            else if (replicationServer.getDataReplication())
245
            {
246
              // If the docid's home server is local host, replicate the docid
247
              if (homeServerCode==1)
248
              {
249
                MetacatReplication.replLog("force data replicating to "
250
                                                                      + server);
251
                comeAndGetIt = new URL("https://" + server +
252
                                 "?action=forcereplicatedatafile&server=" +
253
                                 util.getLocalReplicationServerName() +
254
                                 "&docid=" + docid + "&dbaction=" +
255
                                  action+"&ipAddress="+ipAddress);
256
                //over write the url for delete
257
               if (action != null && action.equals(DELETE))
258
               {
259
                 comeAndGetIt = new URL("https://" + server + "?action=" +
260
                                 MetacatReplication.FORCEREPLICATEDELETE +
261
                                 "&docid=" + docid +"&server="+ util.getLocalReplicationServerName()+"&user="+user+"&ipAddress="+ipAddress);
262

    
263
               }
264

    
265
              }//if serverCode==1
266
              else if (replicationServer.getHub()||
267
                server.equals(MetacatReplication.
268
                                    getServerNameForServerCode(homeServerCode)))
269
              {
270
                // If the docid's home server is not local host, but local host
271
                // is a hub of this server, replicate the docid too.
272
                // If the server is homeserver of the docid replication it too
273
                 MetacatReplication.replLog("force data replicating to "
274
                                                                      + server);
275
                comeAndGetIt = new URL("https://" + server +
276
                                 "?action=forcereplicatedatafile&server=" +
277
                                 util.getLocalReplicationServerName() +
278
                                 "&docid=" + docid + "&dbaction=" +
279
                                  action+"&ipAddress="+ipAddress);
280
                //over write the url for delete
281
               if (action != null && action.equals(DELETE))
282
               {
283
                  comeAndGetIt = new URL("https://" + server + "?action=" +
284
                         MetacatReplication.FORCEREPLICATEDELETE +
285
                        "&docid=" + docid+"&server="+ util.getLocalReplicationServerName()+"&user="+user+"&ipAddress="+ipAddress);
286

    
287
               }
288

    
289
              }//else if servercode==1
290
            }//else if data file
291

    
292
          }//if has explicite action
293
          else
294
          { // has implicite action
295
            MetacatReplication.replLog("force replicating (default action) to )"
296
                                                + server);
297
            // this docid is xml documents and can replicate xml doucment
298
            if (xmlDocument && replicationServer.getReplication())
299
            {
300
              // If homeserver of this doicd is local, replicate it
301
              if (homeServerCode ==1)
302
              {
303
                comeAndGetIt = new URL("https://" + server +
304
                                 "?action=forcereplicate&server=" +
305
                                 util.getLocalReplicationServerName()+
306
                                 "&docid=" + docid+"&ipAddress="+ipAddress);
307

    
308
              }//if homeserver ==1
309
              else if (replicationServer.getHub()||
310
                   server.equals(MetacatReplication.
311
                   getServerNameForServerCode(homeServerCode)))
312
              {
313
                // If home server of this docid is not local host, but local
314
                // host is a hub for this server, replicate this doicd
315
                // If the server is homeserver of the docid, replication it too
316
                comeAndGetIt = new URL("https://" + server +
317
                                 "?action=forcereplicate&server=" +
318
                                 util.getLocalReplicationServerName() +
319
                                 "&docid=" + docid+"&ipAddress="+ipAddress);
320

    
321
              }//else if
322

    
323
            }//if xmlDoucment
324
            else if (replicationServer.getDataReplication())
325
            { //It is datafile and server is configured to replicate data file
326

    
327
              //if home server is local host, replicate the data file
328
              if (homeServerCode ==1)
329
              {
330
                comeAndGetIt = new URL("https://" + server +
331
                                 "?action=forcereplicatedatafile&server=" +
332
                                 util.getLocalReplicationServerName() +
333
                                 "&docid=" + docid+"&ipAddress="+ipAddress);
334
              }//if
335
              else if (replicationServer.getHub()||
336
                          server.equals(MetacatReplication.
337
                          getServerNameForServerCode(homeServerCode)))
338
              {
339
                // If home server is not local host, but the local host is a hub
340
                // For this server, replicate the data file
341
                // If the server is homeserver of the docid, replication it too
342
                comeAndGetIt = new URL("https://" + server +
343
                                 "?action=forcereplicatedatafile&server=" +
344
                                 util.getLocalReplicationServerName() +
345
                                 "&docid=" + docid+"&ipAddress="+ipAddress);
346

    
347
              }//else
348
            }//else if
349
          }//else has implicit action
350

    
351
          //Make sure comeAndGetIt is not empty
352
          if ( comeAndGetIt != null && !comeAndGetIt.equals(""))
353
          {
354
            logMetacat.warn("sending message: "
355
                                                + comeAndGetIt.toString());
356
            String message = MetacatReplication.getURLContent(comeAndGetIt);
357
          }
358
          //send out the url.  message is a dummy variable as the target of
359
          //the URL never directly replies to the request.  this simply
360
          //invoces a read request from the server to this local machine.
361
         }//if notification server
362
        }
363
        catch (Exception ee)
364
        {
365
        	logMetacat.error("error in ForceReplicationHandler.run for server " 
366
                    + server +" "+ee.getMessage());
367
             ee.printStackTrace();
368
        }
369
      }//for
370

    
371
   
372
      logMetacat.warn("exiting ForceReplicationHandler Thread");
373
  }//run
374
}//ForceReplication class
(39-39/66)