Changeset 277:6a9619c93177
- Timestamp:
- 09/03/11 16:52:50 (5 months ago)
- Branch:
- default
- Convert:
- svn:7c3792e6-d75b-4784-96a6-b298f655ee64/trunk@2711
- Location:
- kraken-logstorage/src/main/java/org/krakenapps/logstorage
- Files:
-
- 2 added
- 5 edited
-
DiskLackAction.java (added)
-
DiskSpaceType.java (added)
-
LogStorage.java (modified) (1 diff)
-
engine/Constants.java (modified) (2 diffs)
-
engine/LogStorageEngine.java (modified) (10 diffs)
-
query/parser/SearchParser.java (modified) (1 diff)
-
script/LogStorageScript.java (modified) (2 diffs)
Legend:
- Unmodified
- Added
- Removed
-
kraken-logstorage/src/main/java/org/krakenapps/logstorage/LogStorage.java
r250 r277 43 43 void write(Collection<Log> logs); 44 44 45 int getMinFreeSpaceValue(); 46 47 DiskSpaceType getMinFreeSpaceType(); 48 49 void setMinFreeSpace(int value, DiskSpaceType type); 50 51 DiskLackAction getDiskLackAction(); 52 53 void setDiskLackAction(DiskLackAction action); 54 45 55 Log getLog(LogKey logKey); 46 56 -
kraken-logstorage/src/main/java/org/krakenapps/logstorage/engine/Constants.java
r2 r277 19 19 LogMaxIdleTime("log_max_idle_time"), 20 20 LogFlushInterval("log_flush_interval"), 21 LogMaxBuffering("log_max_buffering"); 21 LogMaxBuffering("log_max_buffering"), 22 MinFreeDiskSpaceType("min_free_disk_space_type", "string"), 23 MinFreeDiskSpaceValue("min_free_disk_space_value"), 24 DiskLackAction("disk_lack_action", "string"); 22 25 23 26 Constants(String name) { … … 36 39 return name; 37 40 } 38 41 39 42 public String getType() { 40 43 return type; -
kraken-logstorage/src/main/java/org/krakenapps/logstorage/engine/LogStorageEngine.java
r255 r277 41 41 import org.apache.felix.ipojo.annotations.Validate; 42 42 import org.krakenapps.codec.EncodingRule; 43 import org.krakenapps.logstorage.DiskLackAction; 44 import org.krakenapps.logstorage.DiskSpaceType; 43 45 import org.krakenapps.logstorage.Log; 44 46 import org.krakenapps.logstorage.LogCallback; … … 62 64 private static final int DEFAULT_MAX_LOG_BUFFERING = 10000; 63 65 private static final int DEFAULT_LOG_FLUSH_INTERVAL = 3600000; 66 private static final String DEFAULT_MIN_FREE_SPACE_TYPE = DiskSpaceType.Percentage.toString(); 67 private static final int DEFAULT_MIN_FREE_SPACE_VALUE = 10; 68 private static final String DEFAULT_DISK_LACK_ACTION = DiskLackAction.StopLogging.toString(); 64 69 65 70 private LogStorageStatus status = LogStorageStatus.Closed; … … 80 85 private Thread writerSweeperThread; 81 86 87 private static final File logDir = new File(System.getProperty("kraken.data.dir"), "kraken-logstorage/log"); 88 private DiskSpaceType minFreeSpaceType; 89 private int minFreeSpaceValue; 90 private DiskLackAction diskLackAction; 91 92 static { 93 logDir.mkdirs(); 94 } 95 82 96 public LogStorageEngine() { 83 97 int maxIdleTime = getIntParameter(Constants.LogMaxIdleTime, DEFAULT_MAX_IDLE_TIME); … … 87 101 writerSweeper = new WriterSweeper(maxIdleTime, flushInterval); 88 102 callbacks = new CopyOnWriteArraySet<LogCallback>(); 103 minFreeSpaceType = DiskSpaceType.valueOf(getStringParameter(Constants.MinFreeDiskSpaceType, 104 DEFAULT_MIN_FREE_SPACE_TYPE)); 105 minFreeSpaceValue = getIntParameter(Constants.MinFreeDiskSpaceValue, DEFAULT_MIN_FREE_SPACE_VALUE); 106 diskLackAction = DiskLackAction.valueOf(getStringParameter(Constants.DiskLackAction, DEFAULT_DISK_LACK_ACTION)); 107 } 108 109 private String getStringParameter(Constants key, String defaultValue) { 110 String value = ConfigUtil.get(prefsvc, key); 111 if (value != null) 112 return value; 113 return defaultValue; 89 114 } 90 115 … … 229 254 dates.add(dateFormat.parse(file.getName().split("\\.")[0])); 230 255 } catch (ParseException e) { 231 e.printStackTrace(); 232 } 233 } 234 } 235 236 Collections.sort(dates, new Comparator<Date>() { 237 @Override 238 public int compare(Date o1, Date o2) { 239 return (int) (o2.getTime() - o1.getTime()); 240 } 241 }); 256 logger.error("kraken logstorage: invalid log filename, table {}, {}", tableName, file.getName()); 257 } 258 } 259 } 260 261 Collections.sort(dates, Collections.reverseOrder()); 242 262 243 263 return dates; … … 295 315 LogRecord logdata = new LogRecord(log.getDate(), log.getId(), bb); 296 316 return logdata; 317 } 318 319 @Override 320 public int getMinFreeSpaceValue() { 321 return minFreeSpaceValue; 322 } 323 324 @Override 325 public DiskSpaceType getMinFreeSpaceType() { 326 return minFreeSpaceType; 327 } 328 329 @Override 330 public void setMinFreeSpace(int value, DiskSpaceType type) { 331 if (type == DiskSpaceType.Percentage) { 332 if (value <= 0 || value >= 100) 333 throw new IllegalArgumentException("invalid value"); 334 } else if (type == DiskSpaceType.Megabyte) { 335 if (value <= 0) 336 throw new IllegalArgumentException("invalid value"); 337 } else if (type == null) 338 throw new IllegalArgumentException("type cannot be null"); 339 340 this.minFreeSpaceType = type; 341 this.minFreeSpaceValue = value; 342 343 ConfigUtil.set(prefsvc, Constants.MinFreeDiskSpaceType, type.toString()); 344 ConfigUtil.set(prefsvc, Constants.MinFreeDiskSpaceValue, Integer.toString(value)); 345 } 346 347 @Override 348 public DiskLackAction getDiskLackAction() { 349 return diskLackAction; 350 } 351 352 @Override 353 public void setDiskLackAction(DiskLackAction action) { 354 if (action == null) 355 throw new IllegalArgumentException("action cannot be null"); 356 357 this.diskLackAction = action; 358 359 ConfigUtil.set(prefsvc, Constants.DiskLackAction, action.toString()); 297 360 } 298 361 … … 593 656 @Override 594 657 public void reload() { 595 int flushInterval = Integer.valueOf(ConfigUtil.get(prefsvc, Constants.LogFlushInterval));596 int maxIdleTime = Integer.valueOf(ConfigUtil.get(prefsvc, Constants.LogMaxIdleTime));658 int flushInterval = getIntParameter(Constants.LogFlushInterval, DEFAULT_LOG_FLUSH_INTERVAL); 659 int maxIdleTime = getIntParameter(Constants.LogMaxIdleTime, DEFAULT_MAX_IDLE_TIME); 597 660 writerSweeper.setFlushInterval(flushInterval); 598 661 writerSweeper.setMaxIdleTime(maxIdleTime); 662 663 minFreeSpaceType = DiskSpaceType.valueOf(getStringParameter(Constants.MinFreeDiskSpaceType, 664 DEFAULT_MIN_FREE_SPACE_TYPE)); 665 minFreeSpaceValue = getIntParameter(Constants.MinFreeDiskSpaceValue, DEFAULT_MIN_FREE_SPACE_VALUE); 666 diskLackAction = DiskLackAction.valueOf(getStringParameter(Constants.DiskLackAction, DEFAULT_DISK_LACK_ACTION)); 599 667 } 600 668 … … 618 686 if (status != LogStorageStatus.Open) 619 687 throw new IllegalStateException("archive not opened"); 688 } 689 690 private class LogFile { 691 private String tableName; 692 private Date date; 693 private File index; 694 private File data; 695 696 private LogFile(String tableName, Date date) { 697 this.tableName = tableName; 698 this.date = date; 699 int tableId = tableRegistry.getTableId(tableName); 700 File tableDir = getTableDirectory(tableId); 701 SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd"); 702 this.index = new File(tableDir, sdf.format(date) + ".idx"); 703 this.data = new File(tableDir, sdf.format(date) + ".dat"); 704 } 705 706 public void remove() { 707 index.delete(); 708 data.delete(); 709 } 710 } 711 712 private class LogFileComparator implements Comparator<LogFile> { 713 @Override 714 public int compare(LogFile o1, LogFile o2) { 715 return o1.date.compareTo(o2.date); 716 } 620 717 } 621 718 … … 652 749 653 750 while (true) { 751 if (isDiskLack()) { 752 logger.warn("kraken logstorage: not enough disk space"); 753 if (diskLackAction == DiskLackAction.StopLogging) { 754 logger.info("kraken logstorage: stop logging"); 755 stop(); 756 } else if (diskLackAction == DiskLackAction.RemoveOldLog) { 757 List<LogFile> files = new ArrayList<LogFile>(); 758 for (String tableName : tableRegistry.getTableNames()) { 759 for (Date date : getLogDates(tableName)) 760 files.add(new LogFile(tableName, date)); 761 } 762 Collections.sort(files, new LogFileComparator()); 763 int index = 0; 764 SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd"); 765 do { 766 if (index >= files.size()) { 767 logger.info("kraken logstorage: stop logging"); 768 stop(); 769 break; 770 } 771 LogFile lf = files.get(index++); 772 logger.info("kraken logstorage: remove old log, table {}, {}", lf.tableName, 773 sdf.format(lf.date)); 774 lf.remove(); 775 } while (isDiskLack()); 776 } 777 } 778 654 779 try { 655 780 if (doStop) … … 707 832 } 708 833 } 834 835 private boolean isDiskLack() { 836 long usable = logDir.getUsableSpace(); 837 long total = logDir.getTotalSpace(); 838 839 logger.trace("kraken logstorage: check disk lack, {} {}", minFreeSpaceValue, minFreeSpaceType); 840 if (minFreeSpaceType == DiskSpaceType.Percentage) { 841 int percent = (int) (usable * 100 / total); 842 if (percent < minFreeSpaceValue) { 843 logger.warn("kraken logstorage: setted minimum free space {}%, now free space {}%", 844 minFreeSpaceValue, percent); 845 return true; 846 } 847 } else if (minFreeSpaceType == DiskSpaceType.Megabyte) { 848 int mega = (int) (usable / 1048576); 849 if (mega < minFreeSpaceValue) { 850 logger.warn("kraken logstorage: setted minimum free space {} MB, now free space {} MB", 851 minFreeSpaceValue, mega); 852 return true; 853 } 854 } 855 856 return false; 857 } 709 858 } 710 859 } -
kraken-logstorage/src/main/java/org/krakenapps/logstorage/query/parser/SearchParser.java
r264 r277 33 33 public void addSyntax(Syntax syntax) { 34 34 // @formatter:off 35 syntax.add("search", this, k(" eval"), ref("option"), rule(new StringPlaceholder(),35 syntax.add("search", this, k("search"), ref("option"), rule(new StringPlaceholder(), 36 36 choice(k("=="), k("!="), k(">"), k("<"), k(">="), k("<="), k("contain"), k("regexp"), k("in")), 37 37 new StringPlaceholder(new char[] {}))); -
kraken-logstorage/src/main/java/org/krakenapps/logstorage/script/LogStorageScript.java
r255 r277 92 92 } 93 93 94 public void reload(String[] args) { 95 storage.reload(); 96 } 97 94 98 @ScriptUsage(description = "create new table", arguments = { @ScriptArgument(name = "name", type = "string", 95 99 description = "log table name") }) … … 202 206 } 203 207 204 @ScriptUsage(description = "set parameters") 208 @ScriptUsage(description = "set parameters", arguments = { 209 @ScriptArgument(name = "key", type = "string", description = "parameter key"), 210 @ScriptArgument(name = "value", type = "string", description = "parameter value") }) 205 211 public void setParameter(String[] args) { 206 212 Constants configKey = Constants.parse(args[0]);
Note: See TracChangeset
for help on using the changeset viewer.
