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: tao $'
14
 *     '$Date: 2005-09-08 16:03:24 -0700 (Thu, 08 Sep 2005) $'
15
 * '$Revision: 2579 $'
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
/**
42
 * A class to asyncronously force the replication of each server
43
 * that has an entry in the xml_replication table.  When run,
44
 * this thread communicates with each server in the list and
45
 * solicites a read of an updated or newly inserted document
46
 * with a certain docid.
47
 */
48
public class ForceReplicationHandler implements Runnable
49
{
50
  private Thread btThread;
51
  private MetaCatUtil util = new MetaCatUtil();
52
  private String docid;
53
  private String action;
54
  private boolean xmlDocument;
55
  private boolean dbactionFlag = true;
56
  private ReplicationServerList serverLists = null;//Serverlist
57
  private int homeServerCode = 0; // home server code for the docid
58
  // When a metacat A got forcereplication
59
  // notification from B, then A will update its record. And A will notification
60
  // other metacat in its serverlist to update this docid if A is a hub. But we
61
  // don't want A to notify B again. B is nofitification of A
62
  private String notificationServer = null;
63

    
64
  //constant
65
  public static final String DELETE = "delete";
66

    
67

    
68

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

    
101
    if(this.action.equals(""))
102
    {
103
      dbactionFlag = false;
104
    }
105

    
106
    btThread = new Thread(this);
107
    btThread.setPriority(Thread.MIN_PRIORITY);
108
    btThread.start();
109
  }
110

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

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

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

    
156
      // Thread seelping for some seconds to make sure the document was insert
157
      // into the database before we send force replication request
158
      int sleepTime = Integer.parseInt
159
                         (MetaCatUtil.getOption("forcereplicationwaitingtime"));
160
      Thread.sleep(sleepTime);
161
      MetaCatUtil.debugMessage("notification server:"+notificationServer, 34);
162
      // Check every server in the serverlists
163
      for (int i=0; i<serverLists.size(); i++)
164
      {
165
        //Set comeAndGetIt null
166
        comeAndGetIt = null;
167
        // Get ReplicationServer object in index i
168
        ReplicationServer replicationServer = serverLists.serverAt(i);
169
       // Get this ReplicationServer 's name
170
        String server = replicationServer.getServerName();
171

    
172
        // If the server is the notification server, we don't notify it back
173
        // again, if server is null we don't replication it
174
        if (server!=null && !server.equals(notificationServer))
175
        {
176

    
177
          if(dbactionFlag)
178
          {
179
            // xml documents and server can replicate xml doucment
180
            if (xmlDocument && replicationServer.getReplication())
181
            {
182
              // If this docid's homeserver is localhost, replicate it
183
              if (homeServerCode==1)
184
              {
185
                MetacatReplication.replLog("force xml replicating to "
186
                                                                    + server);
187
                comeAndGetIt = new URL("https://" + server +
188
                                 "?action=forcereplicate&server=" +
189
                                  util.getLocalReplicationServerName()+
190
                                 "&docid=" + docid + "&dbaction=" +
191
                                  action);
192
                //over write the url for delete
193
                if (action != null && action.equals(DELETE))
194
                {
195
                  comeAndGetIt = new URL("https://" + server + "?action=" +
196
                                   MetacatReplication.FORCEREPLICATEDELETE +
197
                                  "&docid=" + docid +"&server="+ util.getLocalReplicationServerName());
198

    
199
                }
200
              }//if servercode==1
201
              else if (replicationServer.getHub()||
202
                   server.equals(MetacatReplication.
203
                                  getServerNameForServerCode(homeServerCode)))
204
              {
205
                // If the docid's home server is not local host, but local host
206
                // is a hub of this server, replicate the docid too.
207
                // If the server is homeserver of the docid, replication it too
208
                MetacatReplication.replLog("force xml replicating to "
209
                                                                    + server);
210
                comeAndGetIt = new URL("https://" + server +
211
                                 "?action=forcereplicate&server=" +
212
                                 util.getLocalReplicationServerName() +
213
                                 "&docid=" + docid + "&dbaction=" +
214
                                  action);
215
                //over write the url for delete
216
               if (action != null && action.equals(DELETE))
217
               {
218
                  comeAndGetIt = new URL("https://" + server + "?action=" +
219
                            MetacatReplication.FORCEREPLICATEDELETE +
220
                           "&docid=" + docid+"&server="+ util.getLocalReplicationServerName());
221

    
222
                }
223
              }//else
224

    
225
            }//if xmlDocument
226
            //It is data file and configured to handle data replication
227
            else if (replicationServer.getDataReplication())
228
            {
229
              // If the docid's home server is local host, replicate the docid
230
              if (homeServerCode==1)
231
              {
232
                MetacatReplication.replLog("force data replicating to "
233
                                                                      + server);
234
                comeAndGetIt = new URL("https://" + server +
235
                                 "?action=forcereplicatedatafile&server=" +
236
                                 util.getLocalReplicationServerName() +
237
                                 "&docid=" + docid + "&dbaction=" +
238
                                  action);
239
                //over write the url for delete
240
               if (action != null && action.equals(DELETE))
241
               {
242
                 comeAndGetIt = new URL("https://" + server + "?action=" +
243
                                 MetacatReplication.FORCEREPLICATEDELETE +
244
                                 "&docid=" + docid +"&server="+ util.getLocalReplicationServerName());
245

    
246
               }
247

    
248
              }//if serverCode==1
249
              else if (replicationServer.getHub()||
250
                server.equals(MetacatReplication.
251
                                    getServerNameForServerCode(homeServerCode)))
252
              {
253
                // If the docid's home server is not local host, but local host
254
                // is a hub of this server, replicate the docid too.
255
                // If the server is homeserver of the docid replication it too
256
                 MetacatReplication.replLog("force data replicating to "
257
                                                                      + server);
258
                comeAndGetIt = new URL("https://" + server +
259
                                 "?action=forcereplicatedatafile&server=" +
260
                                 util.getLocalReplicationServerName() +
261
                                 "&docid=" + docid + "&dbaction=" +
262
                                  action);
263
                //over write the url for delete
264
               if (action != null && action.equals(DELETE))
265
               {
266
                  comeAndGetIt = new URL("https://" + server + "?action=" +
267
                         MetacatReplication.FORCEREPLICATEDELETE +
268
                        "&docid=" + docid+"&server="+ util.getLocalReplicationServerName());
269

    
270
               }
271

    
272
              }//else if servercode==1
273
            }//else if data file
274

    
275
          }//if has explicite action
276
          else
277
          { // has implicite action
278
            MetacatReplication.replLog("force replicating (default action) to )"
279
                                                + server);
280
            // this docid is xml documents and can replicate xml doucment
281
            if (xmlDocument && replicationServer.getReplication())
282
            {
283
              // If homeserver of this doicd is local, replicate it
284
              if (homeServerCode ==1)
285
              {
286
                comeAndGetIt = new URL("https://" + server +
287
                                 "?action=forcereplicate&server=" +
288
                                 util.getLocalReplicationServerName()+
289
                                 "&docid=" + docid);
290

    
291
              }//if homeserver ==1
292
              else if (replicationServer.getHub()||
293
                   server.equals(MetacatReplication.
294
                   getServerNameForServerCode(homeServerCode)))
295
              {
296
                // If home server of this docid is not local host, but local
297
                // host is a hub for this server, replicate this doicd
298
                // If the server is homeserver of the docid, replication it too
299
                comeAndGetIt = new URL("https://" + server +
300
                                 "?action=forcereplicate&server=" +
301
                                 util.getLocalReplicationServerName() +
302
                                 "&docid=" + docid);
303

    
304
              }//else if
305

    
306
            }//if xmlDoucment
307
            else if (replicationServer.getDataReplication())
308
            { //It is datafile and server is configured to replicate data file
309

    
310
              //if home server is local host, replicate the data file
311
              if (homeServerCode ==1)
312
              {
313
                comeAndGetIt = new URL("https://" + server +
314
                                 "?action=forcereplicatedatafile&server=" +
315
                                 util.getLocalReplicationServerName() +
316
                                 "&docid=" + docid);
317
              }//if
318
              else if (replicationServer.getHub()||
319
                          server.equals(MetacatReplication.
320
                          getServerNameForServerCode(homeServerCode)))
321
              {
322
                // If home server is not local host, but the local host is a hub
323
                // For this server, replicate the data file
324
                // If the server is homeserver of the docid, replication it too
325
                comeAndGetIt = new URL("https://" + server +
326
                                 "?action=forcereplicatedatafile&server=" +
327
                                 util.getLocalReplicationServerName() +
328
                                 "&docid=" + docid);
329

    
330
              }//else
331
            }//else if
332
          }//else has implicit action
333

    
334
          //Make sure comeAndGetIt is not empty
335
          if ( comeAndGetIt != null && !comeAndGetIt.equals(""))
336
          {
337
            MetaCatUtil.debugMessage("sending message: "
338
                                                + comeAndGetIt.toString(), 25);
339
            String message = MetacatReplication.getURLContent(comeAndGetIt);
340
          }
341
          //send out the url.  message is a dummy variable as the target of
342
          //the URL never directly replies to the request.  this simply
343
          //invoces a read request from the server to this local machine.
344
        }//if notification server
345
      }//for
346

    
347
    }//try
348
    catch(Exception e)
349
    {
350
      System.out.println("error in ForceReplicationHandler.run: " +
351
                          e.getMessage());
352
      e.printStackTrace(System.out);
353
    }
354
    MetaCatUtil.debugMessage("exiting ForceReplicationHandler Thread", 25);
355
  }//run
356
}//ForceReplication class
(39-39/63)