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.serviceregistry.impl.jpa;
23  
24  import org.opencastproject.serviceregistry.api.HostRegistration;
25  
26  import javax.persistence.Access;
27  import javax.persistence.AccessType;
28  import javax.persistence.Column;
29  import javax.persistence.Entity;
30  import javax.persistence.GeneratedValue;
31  import javax.persistence.Id;
32  import javax.persistence.Index;
33  import javax.persistence.NamedQueries;
34  import javax.persistence.NamedQuery;
35  import javax.persistence.Table;
36  import javax.persistence.UniqueConstraint;
37  
38  /**
39   * A record of a host providing Opencast services.
40   */
41  @Entity(name = "HostRegistration")
42  @Access(AccessType.FIELD)
43  @Table(
44      name = "oc_host_registration",
45      indexes = {
46          @Index(name = "IX_oc_host_registration_online", columnList = ("online")),
47          @Index(name = "IX_oc_host_registration_active", columnList = ("active")),
48      },
49      uniqueConstraints = @UniqueConstraint(columnNames = "host")
50  )
51  @NamedQueries({
52      @NamedQuery(
53          name = "HostRegistration.getMaxLoad",
54          query = "SELECT sum(hr.maxLoad) FROM HostRegistration hr where hr.active = true"
55      ),
56      @NamedQuery(
57          name = "HostRegistration.getMaxLoadByHostName",
58          query = "SELECT hr.maxLoad FROM HostRegistration hr "
59              + "where hr.baseUrl = :host and hr.active = true"
60      ),
61      @NamedQuery(
62          name = "HostRegistration.byHostName",
63          query = "SELECT hr from HostRegistration hr where hr.baseUrl = :host"
64      ),
65      @NamedQuery(
66          name = "HostRegistration.jobStatistics",
67          query = "select j.processorServiceRegistration.hostRegistration.id, j.status, count(j) "
68              + "from Job j "
69              + "where j.status in :status "
70              + "group by j.processorServiceRegistration.hostRegistration.id, j.status"
71      ),
72      @NamedQuery(
73          name = "HostRegistration.getAll",
74          query = "SELECT hr FROM HostRegistration hr where hr.active = true"
75      ),
76  })
77  public class HostRegistrationJpaImpl implements HostRegistration {
78  
79    @Id
80    @Column(name = "id")
81    @GeneratedValue
82    private Long id;
83  
84    @Column(name = "host", nullable = false)
85    private String baseUrl;
86  
87    @Column(name = "address", nullable = false, length = 39)
88    private String ipAddress;
89  
90    @Column(name = "node_name")
91    private String nodeName;
92  
93    @Column(name = "memory", nullable = false)
94    private long memory;
95  
96    @Column(name = "cores", nullable = false)
97    private int cores;
98  
99    /**
100    * The maximum load this host can run.  This is not necessarily 1-to-1 with the number of jobs.
101    */
102   @Column(name = "max_load", nullable = false)
103   private float maxLoad;
104 
105   @Column(name = "online", nullable = false)
106   private boolean online = true;
107 
108   @Column(name = "active", nullable = false)
109   private boolean active = true;
110 
111   @Column(name = "maintenance", nullable = false)
112   private boolean maintenanceMode = false;;
113 
114   /**
115    * Creates a new host registration which is online
116    */
117   public HostRegistrationJpaImpl() {
118   }
119 
120   public HostRegistrationJpaImpl(
121       String baseUrl,
122       String address,
123       String nodeName,
124       long memory,
125       int cores,
126       float maxLoad,
127       boolean online,
128       boolean maintenance
129   ) {
130     this.baseUrl = baseUrl;
131     this.ipAddress = address;
132     this.nodeName = nodeName;
133     this.memory = memory;
134     this.cores = cores;
135     this.maxLoad = maxLoad;
136     this.online = online;
137     this.maintenanceMode = maintenance;
138     this.active = true;
139   }
140 
141   @Override
142   public Long getId() {
143     return id;
144   }
145 
146   public void setId(Long id) {
147     this.id = id;
148   }
149 
150   @Override
151   public String getBaseUrl() {
152     return baseUrl;
153   }
154 
155   @Override
156   public void setBaseUrl(String baseUrl) {
157     this.baseUrl = baseUrl;
158   }
159 
160   @Override
161   public String getIpAddress() {
162     return ipAddress;
163   }
164 
165   @Override
166   public void setIpAddress(String address) {
167     this.ipAddress = address;
168   }
169 
170   @Override
171   public String getNodeName() {
172     return nodeName;
173   }
174 
175   @Override
176   public void setNodeName(String nodeName) {
177     this.nodeName = nodeName;
178   }
179 
180   @Override
181   public long getMemory() {
182     return memory;
183   }
184 
185   @Override
186   public void setMemory(long memory) {
187     this.memory = memory;
188   }
189 
190   @Override
191   public int getCores() {
192     return cores;
193   }
194 
195   @Override
196   public void setCores(int cores) {
197     this.cores = cores;
198   }
199 
200   @Override
201   public float getMaxLoad() {
202     return maxLoad;
203   }
204 
205   @Override
206   public void setMaxLoad(float maxLoad) {
207     this.maxLoad = maxLoad;
208   }
209 
210   @Override
211   public boolean isOnline() {
212     return online;
213   }
214 
215   @Override
216   public void setOnline(boolean online) {
217     this.online = online;
218   }
219 
220   @Override
221   public boolean isActive() {
222     return active;
223   }
224 
225   @Override
226   public void setActive(boolean active) {
227     this.active = active;
228   }
229 
230   @Override
231   public boolean isMaintenanceMode() {
232     return maintenanceMode;
233   }
234 
235   @Override
236   public void setMaintenanceMode(boolean maintenanceMode) {
237     this.maintenanceMode = maintenanceMode;
238   }
239 
240   @Override
241   public int hashCode() {
242     return toString().hashCode();
243   }
244 
245   @Override
246   public boolean equals(Object obj) {
247     if (!(obj instanceof HostRegistration)) {
248       return false;
249     }
250     HostRegistration registration = (HostRegistration) obj;
251     return baseUrl.equals(registration.getBaseUrl());
252   }
253 
254   @Override
255   public String toString() {
256     return baseUrl;
257   }
258 
259 }