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: 2002-07-14 12:33:25 -0700 (Sun, 14 Jul 2002) $'
15
 * '$Revision: 1292 $'
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
  
65
  
66
  /**
67
   * Constructor of ForceReplicationHandler
68
   * @param docid the docid to force replicate
69
   * @param the action that is being performed on the document (either 
70
   *        INSERT or UPDATE)
71
   * @param xml the docid is a xml document or not (data file)
72
   * @param notificationServer, when a metacat A got forcereplication 
73
   * notification from B, then A will update its record. And A will notification
74
   * other metacat in its serverlist to update this docid if A is a hub. But we
75
   * don't want A to notify B again. B is nofitification of A.
76
   */
77
  public ForceReplicationHandler(String docid, String action, boolean xml, 
78
                                                   String myNotificationServer)
79
  {
80
    this.docid = docid;
81
    this.action = action;
82
    this.xmlDocument =xml;
83
    // Build a severLists from xml_replication table
84
    this.serverLists = new ReplicationServerList();
85
    // Get sever code for this docid
86
    try
87
    {
88
      this.homeServerCode = MetacatReplication.getHomeServerCodeForDocId(docid);
89
    }//try
90
    catch (Exception e)
91
    {
92
      MetaCatUtil.debugMessage("Error in ForceReplicationHandle(): "
93
                                                          +e.getMessage(), 30);
94
    }//catch
95
    // Get the notification server
96
    this.notificationServer = myNotificationServer;
97
    
98
    if(this.action.equals(""))
99
    {
100
      dbactionFlag = false;
101
    }
102
    
103
    btThread = new Thread(this);
104
    btThread.setPriority(Thread.MIN_PRIORITY);
105
    btThread.start();
106
  }
107
  
108
  /**
109
   * Use this constructor when the action is implied.
110
   */
111
  public ForceReplicationHandler(String docid, boolean xml, 
112
                                                String myNotificationServer )
113
  {
114
    this.docid = docid;
115
    this.xmlDocument = xml;
116
    dbactionFlag = false;
117
    // Build a severLists from xml_replication table
118
    this.serverLists = new ReplicationServerList();
119
    // Get home server code for this doicd
120
    try
121
    {
122
      this.homeServerCode = MetacatReplication.getHomeServerCodeForDocId(docid);
123
    }
124
     catch (Exception e)
125
    {
126
      MetaCatUtil.debugMessage("Error in ForceReplicationHandle(): "
127
                                                          +e.getMessage(), 30);
128
    }//catch
129
    // Get notification server
130
    this.notificationServer = myNotificationServer;
131
    btThread = new Thread(this);
132
    btThread.setPriority(Thread.MIN_PRIORITY);
133
    btThread.start();
134
  }
135
  
136
  /**
137
   * Method to send force replication command to other server to get 
138
   * a new or updated docid
139
   */
140
  public void run()
141
  {
142
    
143
    try
144
    {
145
      // URL for notifcation
146
      URL comeAndGetIt = null;
147
      // If no server in xml_replication table, metacat don't need do anything
148
      if (serverLists.isEmpty())
149
      {
150
        return;
151
      }
152
      
153
      // Thread seelping for some seconds to make sure the document was insert 
154
      // into the database before we send force replication request
155
      int sleepTime = Integer.parseInt
156
                         (MetaCatUtil.getOption("forcereplicationwaitingtime"));
157
      Thread.sleep(sleepTime);
158
      MetaCatUtil.debugMessage("notification server:"+notificationServer, 34);
159
      // Check every server in the serverlists
160
      for (int i=0; i<serverLists.size(); i++)
161
      {
162
        //Set comeAndGetIt null
163
        comeAndGetIt = null;
164
        // Get ReplicationServer object in index i
165
        ReplicationServer replicationServer = serverLists.serverAt(i);
166
       // Get this ReplicationServer 's name
167
        String server = replicationServer.getServerName();
168
        
169
        // If the server is the notification server, we don't notify it back 
170
        // again, if server is null we don't replication it
171
        if (server!=null && !server.equals(notificationServer))
172
        {
173
          
174
          if(dbactionFlag)
175
          {
176
            // xml documents and server can replicate xml doucment
177
            if (xmlDocument && replicationServer.getReplication())
178
            {
179
              // If this docid's homeserver is localhost, replicate it
180
              if (homeServerCode==1)
181
              {
182
                MetacatReplication.replLog("force xml replicating to " 
183
                                                                    + server);
184
                comeAndGetIt = new URL("https://" + server + 
185
                                 "?action=forcereplicate&server=" + 
186
                                 util.getOption("server") + 
187
                                 util.getOption("replicationpath") +
188
                                 "&docid=" + docid + "&dbaction=" +
189
                                  action);
190
              }//if servercode==1
191
              else if (replicationServer.getHub()||
192
                   server.equals(MetacatReplication.
193
                                  getServerNameForServerCode(homeServerCode)))
194
              {
195
                // If the docid's home server is not local host, but local host
196
                // is a hub of this server, replicate the docid too.
197
                // If the server is homeserver of the docid, replication it too
198
                MetacatReplication.replLog("force xml replicating to " 
199
                                                                    + server);
200
                comeAndGetIt = new URL("https://" + server + 
201
                                 "?action=forcereplicate&server=" + 
202
                                 util.getOption("server") + 
203
                                 util.getOption("replicationpath") +
204
                                 "&docid=" + docid + "&dbaction=" +
205
                                  action);
206
               
207
              }//else
208
             
209
            }//if xmlDocument
210
            //It is data file and configured to handle data replication
211
            else if (replicationServer.getDataReplication())
212
            {
213
              // If the docid's home server is local host, replicate the docid
214
              if (homeServerCode==1)
215
              {
216
                MetacatReplication.replLog("force data replicating to " 
217
                                                                      + server);
218
                comeAndGetIt = new URL("https://" + server + 
219
                                 "?action=forcereplicatedatafile&server=" + 
220
                                 util.getOption("server") + 
221
                                 util.getOption("replicationpath") +
222
                                 "&docid=" + docid + "&dbaction=" +
223
                                  action);
224
              }//if serverCode==1
225
              else if (replicationServer.getHub()||
226
                server.equals(MetacatReplication.
227
                                    getServerNameForServerCode(homeServerCode)))
228
              {
229
                // If the docid's home server is not local host, but local host 
230
                // is a hub of this server, replicate the docid too.
231
                // If the server is homeserver of the docid replication it too
232
                 MetacatReplication.replLog("force data replicating to " 
233
                                                                      + server);
234
                comeAndGetIt = new URL("https://" + server + 
235
                                 "?action=forcereplicatedatafile&server=" + 
236
                                 util.getOption("server") + 
237
                                 util.getOption("replicationpath") +
238
                                 "&docid=" + docid + "&dbaction=" +
239
                                  action);
240
                
241
              }//else if servercode==1
242
            }//else if data file
243
    
244
          }//if has explicite action
245
          else
246
          { // has implicite action
247
            MetacatReplication.replLog("force replicating (default action) to )"
248
                                                + server);
249
            // this docid is xml documents and can replicate xml doucment
250
            if (xmlDocument && replicationServer.getReplication())
251
            {
252
              // If homeserver of this doicd is local, replicate it
253
              if (homeServerCode ==1)
254
              { 
255
                comeAndGetIt = new URL("https://" + server + 
256
                                 "?action=forcereplicate&server=" + 
257
                                 util.getOption("server") + 
258
                                 util.getOption("replicationpath") +
259
                                 "&docid=" + docid);
260
                
261
              }//if homeserver ==1
262
              else if (replicationServer.getHub()||
263
                   server.equals(MetacatReplication.
264
                   getServerNameForServerCode(homeServerCode)))
265
              {
266
                // If home server of this docid is not local host, but local 
267
                // host is a hub for this server, replicate this doicd
268
                // If the server is homeserver of the docid, replication it too
269
                comeAndGetIt = new URL("https://" + server + 
270
                                 "?action=forcereplicate&server=" + 
271
                                 util.getOption("server") + 
272
                                 util.getOption("replicationpath") +
273
                                 "&docid=" + docid);
274
                
275
              }//else if
276
              
277
            }//if xmlDoucment
278
            else if (replicationServer.getDataReplication())          
279
            { //It is datafile and server is configured to replicate data file
280
              
281
              //if home server is local host, replicate the data file
282
              if (homeServerCode ==1)
283
              {
284
                comeAndGetIt = new URL("https://" + server + 
285
                                 "?action=forcereplicatedatafile&server=" + 
286
                                 util.getOption("server") + 
287
                                 util.getOption("replicationpath") +
288
                                 "&docid=" + docid);
289
              }//if
290
              else if (replicationServer.getHub()||
291
                          server.equals(MetacatReplication.
292
                          getServerNameForServerCode(homeServerCode)))
293
              {
294
                // If home server is not local host, but the local host is a hub
295
                // For this server, replicate the data file
296
                // If the server is homeserver of the docid, replication it too
297
                comeAndGetIt = new URL("https://" + server + 
298
                                 "?action=forcereplicatedatafile&server=" + 
299
                                 util.getOption("server") + 
300
                                 util.getOption("replicationpath") +
301
                                 "&docid=" + docid);
302
                
303
              }//else
304
            }//else if
305
          }//else has implicit action
306
          
307
          //Make sure comeAndGetIt is not empty
308
          if ( comeAndGetIt != null && !comeAndGetIt.equals(""))
309
          {
310
            MetaCatUtil.debugMessage("sending message: " 
311
                                                + comeAndGetIt.toString(), 25);
312
            String message = MetacatReplication.getURLContent(comeAndGetIt);
313
          }
314
          //send out the url.  message is a dummy variable as the target of
315
          //the URL never directly replies to the request.  this simply
316
          //invoces a read request from the server to this local machine.
317
        }//if notification server
318
      }//for
319
     
320
    }//try
321
    catch(Exception e)
322
    {
323
      System.out.println("error in ForceReplicationHandler.run: " + 
324
                          e.getMessage());
325
      e.printStackTrace(System.out);
326
    }
327
    MetaCatUtil.debugMessage("exiting ForceReplicationHandler Thread", 25);
328
  }//run
329
}//ForceReplication class
(36-36/58)