Project

General

Profile

« Previous | Next » 

Revision 1292

Added by Jing Tao almost 22 years ago

Merge cvs branch replication to cvs head.

View differences:

DocumentImpl.java
91 91
  private long rootnodeid;
92 92
  private ElementNode rootNode = null;
93 93
  private TreeSet nodeRecordList = null;
94
  //private static 
95
  //ReplicationServerList serverList = new ReplicationServerList();
94 96
  
95 97
  /**
96 98
   * Constructor used to create a document and read the document information
......
412 414
        deleteXMLDocuments(docIdWithoutRev);
413 415
        ac = new AccessionNumber(accnum, "update");
414 416
      }
417
      // local server has newer version, then notify the remote server
418
      else if ( userSpecifyRev < revInDataBase && revInDataBase > 0)
419
      {
420
        throw new Exception("Local server: "+MetaCatUtil.getOption("server")+
421
                 " has newer revision of doc: "+docIdWithoutRev+"."
422
                  +revInDataBase+". Please notify it.");
423
      }
415 424
      //other situation
416 425
      else
417 426
      {
......
442 451
      sql.append(sqlDateString).append(",");
443 452
      sql.append(sqlDateString).append(",");
444 453
      sql.append("'0')");
454
      // Set auto commit fasle
455
      dbconn.setAutoCommit(false);
445 456
      pstmt = dbconn.prepareStatement(sql.toString());
446 457
      pstmt.execute();
458
      // Commit the insert
459
      dbconn.commit();
447 460
      pstmt.close();
448 461
      //dbconn.close();
449 462
    } 
450 463
    finally 
451 464
    {
465
      // Set DBConnection auto commit true
466
      dbconn.setAutoCommit(true);
452 467
      pstmt.close();
453 468
      DBConnectionPool.returnDBConnection(dbconn, serialNumber);
454 469
    }    
......
456 471
  
457 472
 /**
458 473
   * This method will register a data file entry in xml_documents and save a
459
   * data file input Stream into file system.. It is only used in force 
460
   * replication
474
   * data file input Stream into file system.. It is only used in replication
461 475
   *
462 476
   * @param  input, the input stream which contain the file content.
463 477
   * @param  , the input stream which contain the file content
......
467 481
   *               includes a revision number for this revision of the document 
468 482
   *               (e.g., knb.1.1)
469 483
   * @param user the user that owns the document
470
   * @param serverCode the serverid from xml_replication on which this document
471
   *        resides.
484
   * @param docHomeServer, the home server of the docid
485
   * @param notificationServer, the server to notify force replication info to 
486
   *                            local metacat
472 487
   */
473
 public static void writeDataFile(InputStream input, String filePath, 
474
                                  String docname, String doctype, String accnum, 
475
                                  String user, String docHomeServer)
488
 public static void writeDataFileInReplication(InputStream input, 
489
                 String filePath, String docname, String doctype, String accnum, 
490
                   String user, String docHomeServer, String notificationServer)
476 491
                     throws SQLException, AccessionNumberException, Exception
477 492
 {
478
   int serverCode=-2;
493
    int serverCode=-2;
494
   
495
    
479 496
    if (filePath==null||filePath.equals(""))
480 497
    {
481 498
      throw new 
......
486 503
      throw new Exception("Please specify the stored file name");
487 504
    }
488 505
    
489
    //get server code for the home server
490
    serverCode=getServerCode(docHomeServer);
491
    //if the server is not in the xml replication table, insert it.
492
    if (serverCode==-1)
493
    {
494
      insertServerIntoReplicationTable(docHomeServer);
495
      //get server code again
496
      serverCode=getServerCode(docHomeServer);
497
    }
506
   
498 507
    
499
    //make sure user have file lock grant(local metacat means have it too)
500
    //if (getDataFileLockGrant(accnum))
501
    //{
502
      //register data file into xml_documents table
503
      registerDocumentInReplication(docname, doctype, accnum, user, serverCode);
504
      //write inputstream into file system.
505
      File dataDirectory = new File(filePath);
506
      File newFile = new File(dataDirectory, accnum); 
508
    // If server is not int the xml replication talbe, insert it into 
509
    // xml_replication table
510
    //serverList.addToServerListIfItIsNot(docHomeServer);
511
    insertServerIntoReplicationTable(docHomeServer);
512
    
513
    // Get server code again
514
    serverCode = getServerCode(docHomeServer);
515
    
516
    
517
    //register data file into xml_documents table
518
    registerDocumentInReplication(docname, doctype, accnum, user, serverCode);
519
    //write inputstream into file system.
520
    File dataDirectory = new File(filePath);
521
    File newFile = new File(dataDirectory, accnum); 
507 522
       
508
      // create a buffered byte output stream
509
      // that uses a default-sized output buffer
510
      FileOutputStream fos = new FileOutputStream(newFile);
511
      BufferedOutputStream outPut = new BufferedOutputStream(fos);
523
    // create a buffered byte output stream
524
    // that uses a default-sized output buffer
525
    FileOutputStream fos = new FileOutputStream(newFile);
526
    BufferedOutputStream outPut = new BufferedOutputStream(fos);
512 527

  
513
      BufferedInputStream bis = null;
514
      bis = new BufferedInputStream(input);
515
      byte[] buf = new byte[4 * 1024]; // 4K buffer
516
      int b = bis.read(buf);
528
    BufferedInputStream bis = null;
529
    bis = new BufferedInputStream(input);
530
    byte[] buf = new byte[4 * 1024]; // 4K buffer
531
    int b = bis.read(buf);
517 532
       
518
      while (b != -1) 
519
      {
533
    while (b != -1) 
534
    {
520 535
        outPut.write(buf, 0, b);
521 536
        b = bis.read(buf);
522
      }
523
      bis.close();
524
	    outPut.close();
525
	    fos.close();
526
    
527
    //}//if
537
    }
538
    bis.close();
539
	  outPut.close();
540
	  fos.close();
541
      
542
    // Force replicate data file
543
    ForceReplicationHandler forceReplication = new ForceReplicationHandler
544
                                    (accnum, false, notificationServer);
545
  
528 546
 }
529 547
  
530 548

  
......
579 597
      //saying that he/she needs to download a new copy of the file and
580 598
      //merge the differences manually.
581 599
      
582
      String server = MetacatReplication.getServer(serverCode);
600
      String server=MetacatReplication.getServerNameForServerCode(serverCode);
583 601
      MetacatReplication.replLog("attempting to lock " + accnum);
584 602
      URL u = new URL("https://" + server + "?server=" +
585 603
        MetaCatUtil.getLocalReplicationServerName()+"&action=getlock&updaterev=" 
......
967 985
                            "the latest revision number.");
968 986
        }
969 987
      }
970
      throw new Exception("the requested docid '" + docid.toString() + 
971
                        "' does not exist");
988
      // Get miss docid and rev, throw to McdDocNotFoundException
989
      String missDocId = MetaCatUtil.getDocIdFromString(docid.toString());
990
      String missRevision = 
991
                  MetaCatUtil.getRevisionStringFromString(docid.toString());
992
      throw new McdbDocNotFoundException("the requested docid '" + 
993
                docid.toString() + "' does not exist", missDocId, missRevision);
972 994
    }//try
973 995
    finally
974 996
    {
......
978 1000
  }
979 1001

  
980 1002
  private void getDocumentInfo(String docid) throws McdbException, 
981
                                                    AccessionNumberException
1003
                                        AccessionNumberException, Exception
982 1004
  {
983 1005
    getDocumentInfo(new DocumentIdentifier(docid));
984 1006
  }
......
988 1010
   *
989 1011
   * @param docid the id of the document to look up
990 1012
   */
991
  private void getDocumentInfo(DocumentIdentifier docid) throws McdbException 
1013
  private void getDocumentInfo(DocumentIdentifier docid) throws McdbException
1014
                                                          , Exception
992 1015
  {
993 1016
    DBConnection dbconn = null;
994 1017
    int serialNumber = -1;
......
1002 1025
        table = "xml_revisions";
1003 1026
      }
1004 1027
    }
1005
    catch(Exception e)
1028
    // catch a McdbDocNotFoundException throw it
1029
    catch (McdbDocNotFoundException notFound)
1006 1030
    {
1007
      System.out.println("error in DocumentImpl.getDocumentInfo: " + 
1008
                          e.getMessage());
1031
      throw notFound;
1009 1032
    }
1010
    
1011
    //deal with the key words here.
1012
    
1013
    if(docid.getRev().equals("all"))
1033
    catch(Exception e)
1014 1034
    {
1015 1035
      
1036
      MetaCatUtil.debugMessage("error in DocumentImpl.getDocumentInfo: " + 
1037
                          e.getMessage(), 30);
1038
      throw e;
1016 1039
    }
1017 1040
    
1041

  
1042
    
1018 1043
    try 
1019 1044
    {
1020 1045
      dbconn=DBConnectionPool.getDBConnection("DocumentImpl.getDocumentInfo");
......
1124 1149
    }
1125 1150

  
1126 1151
    if (this.docname == null) {
1127
      throw new McdbDocNotFoundException("Document not found: " + docid);
1152
      throw new McdbDocNotFoundException("Document not found: " + docid,
1153
                                 docid.getIdentifier(), docid.getRev());
1128 1154
    }
1129 1155
  }
1130 1156

  
......
1270 1296

  
1271 1297
        // Save the old document publicaccessentry in a backup table
1272 1298
        DocumentImpl.archiveDocRevision(connection, docid, user );
1273
        DocumentImpl thisdoc = new DocumentImpl(docid);
1299
        DocumentImpl thisdoc = new DocumentImpl(docid, false);
1274 1300
        int thisrev = thisdoc.getRev();
1275 1301
        
1276 1302
        //if the updated vesion is not greater than current one,
......
1474 1500
  
1475 1501
      DocumentIdentifier id = new DocumentIdentifier(accnum);
1476 1502
      String updaterev = id.getRev();
1477
      String server = MetacatReplication.getServer(serverCode);
1503
      String server=MetacatReplication.getServerNameForServerCode(serverCode);
1478 1504
      MetacatReplication.replLog("attempting to lock " + accnum);
1479 1505
      URL u = new URL("https://" + server + "?server="+
1480 1506
        MetaCatUtil.getLocalReplicationServerName()+"&action=getlock&updaterev=" 
......
1505 1531
                
1506 1532
        
1507 1533
        
1508
        //If Metacat is super hub, tell all servers in its server list to get 
1509
        //the new document, ture mean it is xml document
1510
        if ((MetaCatUtil.getOption("hub")).equals("super"))
1511
        {
1512
          ForceReplicationHandler frh = new ForceReplicationHandler(accnum, 
1513
                true, ReplicationHandler.buildServerList());
1514
        }
1515
        else
1516
        {
1517
          //after inserting the document locally, tell the document's homeserver
1518
          //to come get a copy from here.
1519
         
1520
          ForceReplicationHandler frh = new ForceReplicationHandler(accnum, 
1521
                true, ReplicationHandler.getHomeServer(docid));
1522
        }
1523
        return (accnum);
1534
        // Force replication the docid
1535
        ForceReplicationHandler frh = new ForceReplicationHandler
1536
                                                          (accnum, true, null);
1537
        return(accnum);
1538
   
1524 1539
      }
1525 1540

  
1526 1541
      else if(openingtag.equals("<filelocked>"))
......
1574 1589
      throw e;
1575 1590
    }
1576 1591
    
1577
    //force replicate out the new document to each server in our server list.
1578
    if(serverCode == 1)
1579
    { 
1592
    // Force replicate out the new document to each server in our server list.
1593
    // Start the thread to replicate this new document out to the other servers
1594
    // true mean it is xml document
1595
    // null is because no metacat notify the force replication.
1596
    ForceReplicationHandler frh = new ForceReplicationHandler
1597
                                                  (accnum, action, true, null);
1580 1598
      
1581
      //start the thread to replicate this new document out to the other servers
1582
      //true mean it is xml document
1583
      ForceReplicationHandler frh = new ForceReplicationHandler
1584
                (accnum, action, true,ReplicationHandler.buildServerList());
1585
      
1586
    }
1599
   
1587 1600
    MetaCatUtil.debugMessage("Conn Usage count after writting: "
1588 1601
                                                      +conn.getUsageCount(),50); 
1589 1602
    return(accnum);
......
1602 1615
   * @param groups the groups to which user belongs
1603 1616
   * @param homeServer the name of server which the document origanlly create
1604 1617
   * @param validate, if the xml document is valid or not
1618
   * @param notifyServer, the server which notify local server the force 
1619
   *                       replication command
1605 1620
   */
1606 1621

  
1607 1622
  public static String writeReplication(DBConnection conn, Reader xml, 
1608 1623
            String pub, Reader dtd, String action, String accnum, String user,
1609
            String[] groups,String homeServer, boolean validate)
1624
     String[] groups,String homeServer, boolean validate, String notifyServer)
1610 1625
                throws Exception
1611 1626
  {
1627
    // Docid without revision
1628
    String docid=MetaCatUtil.getDocIdFromString(accnum);
1629
    // Revision specified by user (int)
1630
    int userSpecifyRev=MetaCatUtil.getVersionFromString(accnum);
1631
    // Revision for this docid in current database
1632
    int revInDataBase=getLatestRevisionNumber(docid);
1633
    // String to store the revision
1634
    String rev = null;
1612 1635
   
1613
    String docid=MetaCatUtil.getDocIdFromString(accnum);
1614
    String rev=(new Integer(MetaCatUtil.getVersionFromString(accnum))).
1615
                                                                    toString();
1616 1636
    
1617
    //check if xml_documents table has the document or not
1618
    //if not, set action to insert
1619
    if (getLatestRevisionNumber(docid)==-1)
1637
    //revIndataBase=-1, there is no record in xml_documents table
1638
    //the document is a new one for local server, inert it into table
1639
    //user specified rev should be great than 0
1640
    if (revInDataBase==-1 && userSpecifyRev>0 )
1620 1641
    {
1621
      action="INSERT";
1642
        // rev equals user specified
1643
        rev=(new Integer(userSpecifyRev)).toString();
1644
        // action should be INSERT
1645
        action = "INSERT";
1622 1646
    }
1623
    
1624
    int serverCode=-2;
1625
    //get server code for the home server
1626
    serverCode=getServerCode(homeServer);
1627
    //if the server is not in the xml replication table, insert it.
1628
    if (serverCode==-1)
1647
    //rev is greater the last revsion number and revInDataBase isn't -1
1648
    // it is a updated  file
1649
    else if (userSpecifyRev>revInDataBase && revInDataBase>0)
1629 1650
    {
1630
      insertServerIntoReplicationTable(homeServer);
1631
      //get server code again
1632
      serverCode=getServerCode(homeServer);
1651
       // rev equals user specified
1652
       rev=(new Integer(userSpecifyRev)).toString();
1653
       // action should be update
1654
       action = "UPDATE";
1633 1655
    }
1634
      
1635
    MetaCatUtil.debugMessage("action: " + action + " servercode: " + 
1636
                             serverCode, 10);
1656
    // local server has newer version, then notify the remote server
1657
    else if ( userSpecifyRev < revInDataBase && revInDataBase > 0)
1658
    {
1659
      throw new Exception("Local server: "+MetaCatUtil.getOption("server")+
1660
                " has newer revision of doc: "+docid+"."+revInDataBase
1661
                 +". Please notify it.");
1662
    }
1663
    //other situation
1664
    else
1665
    {
1666
        
1667
        throw new Exception("The docid"+docid+"'s revision number couldn't be "
1668
                    +userSpecifyRev);
1669
    }
1670
    // Variable to store homeserver code
1671
    int serverCode=-2;
1672
    
1673
     // If server is not int the xml replication talbe, insert it into
1674
    // xml_replication table
1675
    //serverList.addToServerListIfItIsNot(homeServer);
1676
    insertServerIntoReplicationTable(homeServer);
1677
    // Get server code again
1678
    serverCode = getServerCode(homeServer);
1679
    
1680
    
1681
    MetaCatUtil.debugMessage("Document "+docid+"."+rev+" "+action+ " into local"
1682
                               +" metacat with servercode: "+ serverCode, 10);
1637 1683
                        
1638 1684
  
1639

  
1685
    // insert into xml_nodes and xml_index table
1640 1686
    try 
1641 1687
    { 
1642 1688
      
......
1653 1699
      conn.setAutoCommit(true);
1654 1700
      throw e;
1655 1701
    }
1702
    
1703
    //Force replication to other server
1704
    ForceReplicationHandler forceReplication = new ForceReplicationHandler
1705
                                  (accnum, action, true, notifyServer);
1706
    
1656 1707

  
1657 1708
    return(accnum);
1658 1709
  }
......
2220 2271
   * Insert a server into xml_replcation table
2221 2272
   * @param server, the name of server 
2222 2273
   */
2223
  private static void  insertServerIntoReplicationTable(String server)
2274
  private static synchronized void 
2275
                                insertServerIntoReplicationTable(String server)
2224 2276
  {
2225 2277
    PreparedStatement pStmt=null;
2226 2278
    DBConnection dbConn = null;
2227 2279
    int serialNumber = -1;
2228
    int replicate = 1;
2229
    //MetaCatUtil util = new MetaCatUtil();
2280
    
2281
    // Initial value for the server
2282
    int replicate = 0;
2283
    int dataReplicate = 0;
2284
    int hub = 0;
2285
   
2230 2286
    try
2231 2287
    {
2232
       //dbConn=util.openDBConnection();
2233
       //check out DBConnection
2288
       // Get DBConnection
2234 2289
       dbConn=DBConnectionPool.
2235 2290
                getDBConnection("DocumentImpl.insertServIntoReplicationTable");
2236 2291
       serialNumber=dbConn.getCheckOutSerialNumber();
2237
       pStmt = dbConn.prepareStatement("INSERT INTO xml_replication " +
2238
                                      "(server, last_checked, replicate) " +
2239
                                      "VALUES ('" + server + "', to_date(" +
2240
                                      "'01/01/00', 'MM/DD/YY'), '" +
2241
                                      replicate + "')");
2292
      
2293
      // Compare the server to dabase
2294
      pStmt = dbConn.prepareStatement
2295
      ("SELECT serverid FROM xml_replication WHERE server='" + server +"'");
2296
      pStmt.execute();
2297
      ResultSet rs = pStmt.getResultSet();
2298
      boolean hasRow = rs.next();
2299
      // Close preparedstatement and result set
2300
      pStmt.close();
2301
      rs.close();
2302
      
2303
      // If the server is not in the table, and server is not local host,
2304
      // insert it
2305
      if ( !hasRow 
2306
         && !server.equals(MetaCatUtil.getLocalReplicationServerName()))
2307
      {
2308
        // Set auto commit false
2309
        dbConn.setAutoCommit(false);
2310
        pStmt = dbConn.prepareStatement("INSERT INTO xml_replication " +
2311
                      "(server, last_checked, replicate, datareplicate, hub) " +
2312
                       "VALUES ('" + server + "', to_date(" +
2313
                       "'01/01/00', 'MM/DD/YY'), '" +
2314
                       replicate +"', '"+dataReplicate+"','"+ hub + "')");
2242 2315
        pStmt.execute();
2316
        dbConn.commit();
2317
        // Increase usage number
2318
        dbConn.increaseUsageCount(1);
2243 2319
        pStmt.close();
2244
        dbConn.commit();
2320
        
2321
      }
2245 2322
    }//try
2246 2323
    catch (Exception e)
2247 2324
    {
......
2253 2330
     
2254 2331
      try
2255 2332
      {
2333
        // Set auto commit true
2334
        dbConn.setAutoCommit(true);
2256 2335
        pStmt.close();
2336
        
2257 2337
      }//try
2258 2338
      catch (Exception ee)
2259 2339
      {

Also available in: Unified diff