/src/main/java/org/graylog2/database/MongoConnection.java

https://github.com/meddah/graylog2-server · Java · 186 lines · 92 code · 29 blank · 65 comment · 16 complexity · de5be73676fe1fe7f2f5926969edfdf7 MD5 · raw file

  1. /**
  2. * Copyright 2010, 2011 Lennart Koopmann <lennart@socketfeed.com>
  3. *
  4. * This file is part of Graylog2.
  5. *
  6. * Graylog2 is free software: you can redistribute it and/or modify
  7. * it under the terms of the GNU General Public License as published by
  8. * the Free Software Foundation, either version 3 of the License, or
  9. * (at your option) any later version.
  10. *
  11. * Graylog2 is distributed in the hope that it will be useful,
  12. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  14. * GNU General Public License for more details.
  15. *
  16. * You should have received a copy of the GNU General Public License
  17. * along with Graylog2. If not, see <http://www.gnu.org/licenses/>.
  18. *
  19. */
  20. package org.graylog2.database;
  21. import com.mongodb.*;
  22. import java.util.List;
  23. /**
  24. * MongoConnection.java: Jun 6, 2010 1:36:19 PM
  25. *
  26. * MongoDB connection singleton
  27. *
  28. * @author: Lennart Koopmann <lennart@socketfeed.com>
  29. */
  30. public final class MongoConnection {
  31. private static MongoConnection instance;
  32. private Mongo m = null;
  33. private DB db = null;
  34. private DBCollection messagesCollection = null;
  35. private DBCollection historicServerValuesCollection = null;
  36. private long messagesCollSize;
  37. private MongoConnection() {}
  38. /**
  39. * Get the connection instance
  40. * @return MongoConnection instance
  41. */
  42. public synchronized static MongoConnection getInstance() {
  43. if (instance == null) {
  44. instance = new MongoConnection();
  45. }
  46. return instance;
  47. }
  48. /**
  49. * Connect the instance.
  50. *
  51. * @param username MongoDB user
  52. * @param password MongoDB password
  53. * @param hostname MongoDB host
  54. * @param database MongoDB database
  55. * @param port MongoDB port
  56. * @param useAuth Use authentication?
  57. * @param maxConnections
  58. * @param threadsAllowedToBlockForConnectionMultiplier
  59. * @param replicaServers
  60. *
  61. * @throws Exception
  62. */
  63. public void connect(String username, String password, String hostname, String database, int port, String useAuth,
  64. int maxConnections, int threadsAllowedToBlockForConnectionMultiplier,
  65. List<ServerAddress> replicaServers, long messagesCollSize) throws Exception {
  66. MongoOptions options = new MongoOptions();
  67. options.connectionsPerHost = maxConnections;
  68. options.threadsAllowedToBlockForConnectionMultiplier = threadsAllowedToBlockForConnectionMultiplier;
  69. this.messagesCollSize = messagesCollSize;
  70. try {
  71. // Connect to replica servers if given. Else the standard way to one server.
  72. if (replicaServers != null && replicaServers.size() > 0) {
  73. m = new Mongo(replicaServers, options);
  74. } else {
  75. ServerAddress address = new ServerAddress(hostname, port);
  76. m = new Mongo(address, options);
  77. }
  78. db = m.getDB(database);
  79. // Try to authenticate if configured.
  80. if (useAuth.equals("true")) {
  81. if(!db.authenticate(username, password.toCharArray())) {
  82. throw new Exception("Could not authenticate to database '" + database + "' with user '" + username + "'.");
  83. }
  84. }
  85. } catch (MongoException.Network e) {
  86. throw new Exception("Could not connect to Mongo DB. (" + e.toString() + ")");
  87. }
  88. }
  89. /**
  90. * Returns the raw connection.
  91. * @return connection
  92. */
  93. public Mongo getConnection() {
  94. return m;
  95. }
  96. /**
  97. * Returns the raw database object.
  98. * @return database
  99. */
  100. public DB getDatabase() {
  101. return db;
  102. }
  103. /**
  104. * Get the messages collection. Lazily creates a new, capped one based on the
  105. * messages_collection_size from graylog2.conf if there is none.
  106. *
  107. * @return The messages collection
  108. */
  109. public DBCollection getMessagesColl() {
  110. if (this.messagesCollection != null) {
  111. return this.messagesCollection;
  112. }
  113. // Collection has not been cached yet. Do it now.
  114. DBCollection coll = null;
  115. // Create a capped collection if the collection does not yet exist.
  116. if(MongoConnection.getInstance().getDatabase().collectionExists("messages")) {
  117. coll = MongoConnection.getInstance().getDatabase().getCollection("messages");
  118. } else {
  119. coll = MongoConnection.getInstance()
  120. .getDatabase()
  121. .createCollection("messages", BasicDBObjectBuilder.start()
  122. .add("capped", true)
  123. .add("size", messagesCollSize)
  124. .get());
  125. }
  126. coll.ensureIndex(new BasicDBObject("_id", 1));
  127. coll.ensureIndex(new BasicDBObject("created_at", 1));
  128. coll.ensureIndex(new BasicDBObject("host", 1));
  129. coll.ensureIndex(new BasicDBObject("streams", 1));
  130. coll.ensureIndex(new BasicDBObject("facility", 1));
  131. coll.ensureIndex(new BasicDBObject("level", 1));
  132. this.messagesCollection = coll;
  133. return coll;
  134. }
  135. public DBCollection getHistoricServerValuesColl() {
  136. if (this.historicServerValuesCollection != null) {
  137. return this.historicServerValuesCollection;
  138. }
  139. // Collection has not been cached yet. Do it now.
  140. DBCollection coll = null;
  141. // Create a capped collection if the collection does not yet exist.
  142. if(MongoConnection.getInstance().getDatabase().collectionExists("historic_server_values")) {
  143. coll = MongoConnection.getInstance().getDatabase().getCollection("historic_server_values");
  144. } else {
  145. coll = MongoConnection.getInstance()
  146. .getDatabase().createCollection("historic_server_values", BasicDBObjectBuilder.start()
  147. .add("capped", true)
  148. .add("size", 10485760) // 10 MB
  149. .add("max", 720) // Minutes. -> 12 hours.
  150. .get());
  151. }
  152. coll.ensureIndex(new BasicDBObject("type", 1));
  153. coll.ensureIndex(new BasicDBObject("created_at", 1));
  154. this.historicServerValuesCollection = coll;
  155. return coll;
  156. }
  157. }