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
 *    Release: @release@
12
 *
13
 *   '$Author: sgarg $'
14
 *     '$Date: 2005-10-10 11:06:55 -0700 (Mon, 10 Oct 2005) $'
15
 * '$Revision: 2663 $'
16
 *
17
 * This program is free software; you can redistribute it and/or modify
18
 * it under the terms of the GNU General Public License as published by
19
 * the Free Software Foundation; either version 2 of the License, or
20
 * (at your option) any later version.
21
 *
22
 * This program is distributed in the hope that it will be useful,
23
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
24
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
25
 * GNU General Public License for more details.
26
 *
27
 * You should have received a copy of the GNU General Public License
28
 * along with this program; if not, write to the Free Software
29
 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
30
 */
31

    
32
package edu.ucsb.nceas.metacat;
33

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

    
41
import org.apache.log4j.Logger;
42

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

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

    
70

    
71

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

    
104
    if(this.action.equals(""))
105
    {
106
      dbactionFlag = false;
107
    }
108

    
109
    btThread = new Thread(this);
110
    btThread.setPriority(Thread.MIN_PRIORITY);
111
    btThread.start();
112
  }
113

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

    
142
  /**
143
   * Method to send force replication command to other server to get
144
   * a new or updated docid
145
   */
146
  public void run()
147
  {
148

    
149
    
150
      // URL for notifcation
151
      URL comeAndGetIt = null;
152
      // If no server in xml_replication table, metacat don't need do anything
153
      if (serverLists.isEmpty())
154
      {
155
        return;
156
      }
157

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

    
184
        // If the server is the notification server, we don't notify it back
185
        // again, if server is null we don't replication it
186
        if (server!=null && !server.equals(notificationServer))
187
        {
188

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

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

    
234
                }
235
              }//else
236

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

    
258
               }
259

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

    
282
               }
283

    
284
              }//else if servercode==1
285
            }//else if data file
286

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

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

    
316
              }//else if
317

    
318
            }//if xmlDoucment
319
            else if (replicationServer.getDataReplication())
320
            { //It is datafile and server is configured to replicate data file
321

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

    
342
              }//else
343
            }//else if
344
          }//else has implicit action
345

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

    
366
   
367
      logMetacat.warn("exiting ForceReplicationHandler Thread");
368
  }//run
369
}//ForceReplication class
(39-39/63)