View Javadoc
1   /*
2    * Licensed to The Apereo Foundation under one or more contributor license
3    * agreements. See the NOTICE file distributed with this work for additional
4    * information regarding copyright ownership.
5    *
6    *
7    * The Apereo Foundation licenses this file to you under the Educational
8    * Community License, Version 2.0 (the "License"); you may not use this file
9    * except in compliance with the License. You may obtain a copy of the License
10   * at:
11   *
12   *   http://opensource.org/licenses/ecl2.txt
13   *
14   * Unless required by applicable law or agreed to in writing, software
15   * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
16   * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.  See the
17   * License for the specific language governing permissions and limitations under
18   * the License.
19   *
20   */
21  
22  package org.opencastproject.elasticsearch.index.rebuild;
23  
24  import org.opencastproject.security.api.Organization;
25  
26  import org.slf4j.Logger;
27  
28  /**
29   * This implementation of IndexProducer adds logging methods for convenience.
30   */
31  public abstract class AbstractIndexProducer implements IndexProducer {
32  
33    /**
34     * Log beginning of index rebuild for this service.
35     *
36     * @param logger
37     *           An slf4j logger to preserve the context.
38     * @param total
39     *           The total amount of elements to be re-added.
40     * @param elementName
41     *           The elements to be added (e.g. 'events').
42     */
43    protected void logIndexRebuildBegin(Logger logger, int total, String elementName) {
44      logger.info("Starting {} index rebuild with {} {}", getService(), total,
45              elementName);
46    }
47  
48    /**
49     * Log beginning of index rebuild for this service and a specific organization.
50     *
51     * @param logger
52     *           An slf4j logger to preserve the context.
53     * @param total
54     *           The total amount of elements to be re-added.
55     * @param elementName
56     *           The elements to be added (e.g. 'events').
57     * @param org
58     *           The organization.
59     */
60    protected void logIndexRebuildBegin(Logger logger, int total, String elementName,
61            Organization org) {
62      logger.info("Starting {} index rebuild for organization {} with {} {}",
63              getService(), org, total, elementName);
64    }
65  
66    /**
67     * Log the progress of the index rebuild for this service.
68     *
69     * @param logger
70     *           An slf4j logger to preserve the context.
71     * @param total
72     *           The total amount of elements to be re-added.
73     * @param current
74     *           The amount of elements that have already been re-added.
75     */
76    protected void logIndexRebuildProgress(Logger logger, int total, int current) {
77      logIndexRebuildProgress(logger, total, current, 1);
78    }
79  
80    /**
81     * Log the progress of the index rebuild for this service and a specific organization.
82     *
83     * @param logger
84     *           An slf4j logger to preserve the context.
85     * @param total
86     *           The total amount of elements to be re-added.
87     * @param current
88     *           The amount of elements that have already been re-added.
89     * @param org
90     *           The organization.
91     */
92    protected void logIndexRebuildProgress(Logger logger, int total, int current, Organization org) {
93      logIndexRebuildProgress(logger, total, current, 1, org);
94    }
95  
96    /**
97     * Log the progress of the index rebuild for this service.
98     *
99     * @param logger
100    *           An slf4j logger to preserve the context.
101    * @param total
102    *           The total amount of elements to be re-added.
103    * @param current
104    *           The amount of elements that have already been re-added.
105    * @param batchSize
106    *           The size of the batch we re-add in one go.
107    */
108   protected void logIndexRebuildProgress(Logger logger, int total, int current, int batchSize) {
109     logIndexRebuildProgress(logger, total, current, batchSize, null);
110   }
111 
112   /**
113    * Log the progress of the index rebuild for this service.
114    *
115    * @param logger
116    *           An slf4j logger to preserve the context.
117    * @param total
118    *           The total amount of elements to be re-added.
119    * @param current
120    *           The amount of elements that have already been re-added.
121    * @param batchSize
122    *           The size of the batch we re-add in one go.
123    * @param org
124    *           The organization (can be null).
125    */
126   protected void logIndexRebuildProgress(Logger logger, int total, int current, int batchSize,
127           Organization org) {
128     final int responseInterval = (total < 100) ? 1 : (total / 100);
129     if (responseInterval == 1 || batchSize > responseInterval || current == total
130             || current % responseInterval < batchSize) {
131 
132       int progress = total > 0 ? (current * 100 / total) : 100;
133       if (org == null) {
134         logger.info("{} index rebuild: {}/{} finished, {}% complete.", getService(),
135                 current, total, progress);
136       } else {
137         logger.info("{} index rebuild for organization {}: {}/{} finished, {}% complete.",
138                 getService(), org.getId(), current, total, progress);
139       }
140     }
141   }
142 
143   /**
144    * Log an error when one element can't be re-indexed.
145    *
146    * @param logger
147    *           An slf4j logger to preserve the context.
148    * @param elementName
149    *           The name of the element that can't be added (e.g. 'event').
150    * @param element
151    *           The element that can't be added.
152    * @param t
153    *           The error that occurred.
154    */
155   protected void logSkippingElement(Logger logger, String elementName, String element, Throwable t) {
156     logger.error("Unable to re-index {} {}, skipping.", elementName, element, t);
157   }
158 
159   /**
160    * Log an error when one element can't be re-indexed.
161    *
162    * @param logger
163    *           An slf4j logger to preserve the context.
164    * @param elementName
165    *           The name of the element that can't be added (e.g. 'event').
166    * @param element
167    *           The element that can't be added.
168    * @param t
169    *           The error that occurred.
170    * @param org
171    *           The organization.
172    */
173   protected void logSkippingElement(Logger logger, String elementName, String element, Organization org, Throwable t) {
174     logger.error("Unable to re-index {} {} for organization {}, skipping.", elementName, element, org.getId(), t);
175   }
176 
177   /**
178    * Log an error during an index rebuild for this service.
179    *
180    * @param logger
181    *           An slf4j logger to preserve the context.
182    * @param t
183    *           The error that occurred.
184    */
185   protected void logIndexRebuildError(Logger logger, Throwable t) {
186     logger.error("Error updating the {} index.", getService(), t);
187   }
188 
189   /**
190    * Log an error during an index rebuild for this service.
191    *
192    * @param logger
193    *           An slf4j logger to preserve the context.
194    * @param total
195    *           The total amount of elements to be re-added.
196    * @param current
197    *           The amount of elements that have already been re-added.
198    * @param t
199    *           The error that occurred.
200    */
201   protected void logIndexRebuildError(Logger logger, int total, int current, Throwable t) {
202     logger.error("Error updating the {} index: {}/{} could be finished.", getService(),
203             current, total, t);
204   }
205 
206   /**
207    * Log an error during an index rebuild for this service.
208    *
209    * @param logger
210    *           An slf4j logger to preserve the context.
211    * @param t
212    *           The error that occurred.
213    * @param org
214    *           The organization.
215    */
216   protected void logIndexRebuildError(Logger logger, Throwable t, Organization org) {
217     logger.error("Error updating the {} index for service for organization {}.", getService(),
218             org.getId(), t);
219   }
220 }