View Javadoc

1   /*
2    *
3    * Licensed to the Apache Software Foundation (ASF) under one
4    * or more contributor license agreements.  See the NOTICE file
5    * distributed with this work for additional information
6    * regarding copyright ownership.  The ASF licenses this file
7    * to you under the Apache License, Version 2.0 (the
8    * "License"); you may not use this file except in compliance
9    * with the License.  You may obtain a copy of the License at
10   *
11   *     http://www.apache.org/licenses/LICENSE-2.0
12   *
13   * Unless required by applicable law or agreed to in writing, software
14   * distributed under the License is distributed on an "AS IS" BASIS,
15   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16   * See the License for the specific language governing permissions and
17   * limitations under the License.
18   */
19  
20  package org.apache.hadoop.hbase.rest;
21  
22  import java.io.IOException;
23  import java.util.Map;
24  
25  import javax.ws.rs.Consumes;
26  import javax.ws.rs.DELETE;
27  import javax.ws.rs.GET;
28  import javax.ws.rs.POST;
29  import javax.ws.rs.PUT;
30  import javax.ws.rs.Produces;
31  import javax.ws.rs.core.CacheControl;
32  import javax.ws.rs.core.Context;
33  import javax.ws.rs.core.Response;
34  import javax.ws.rs.core.Response.ResponseBuilder;
35  import javax.ws.rs.core.UriInfo;
36  import javax.xml.namespace.QName;
37  
38  import org.apache.commons.logging.Log;
39  import org.apache.commons.logging.LogFactory;
40  import org.apache.hadoop.classification.InterfaceAudience;
41  import org.apache.hadoop.hbase.HColumnDescriptor;
42  import org.apache.hadoop.hbase.HTableDescriptor;
43  import org.apache.hadoop.hbase.TableExistsException;
44  import org.apache.hadoop.hbase.TableName;
45  import org.apache.hadoop.hbase.TableNotFoundException;
46  import org.apache.hadoop.hbase.client.HBaseAdmin;
47  import org.apache.hadoop.hbase.client.HTableInterface;
48  import org.apache.hadoop.hbase.rest.model.ColumnSchemaModel;
49  import org.apache.hadoop.hbase.rest.model.TableSchemaModel;
50  import org.apache.hadoop.hbase.util.Bytes;
51  
52  @InterfaceAudience.Private
53  public class SchemaResource extends ResourceBase {
54    private static final Log LOG = LogFactory.getLog(SchemaResource.class);
55  
56    static CacheControl cacheControl;
57    static {
58      cacheControl = new CacheControl();
59      cacheControl.setNoCache(true);
60      cacheControl.setNoTransform(false);
61    }
62  
63    TableResource tableResource;
64  
65    /**
66     * Constructor
67     * @param tableResource
68     * @throws IOException
69     */
70    public SchemaResource(TableResource tableResource) throws IOException {
71      super();
72      this.tableResource = tableResource;
73    }
74  
75    private HTableDescriptor getTableSchema() throws IOException,
76        TableNotFoundException {
77      HTableInterface table = servlet.getTable(tableResource.getName());
78      try {
79        return table.getTableDescriptor();
80      } finally {
81        table.close();
82      }
83    }
84  
85    @GET
86    @Produces({MIMETYPE_TEXT, MIMETYPE_XML, MIMETYPE_JSON, MIMETYPE_PROTOBUF,
87      MIMETYPE_PROTOBUF_IETF})
88    public Response get(final @Context UriInfo uriInfo) {
89      if (LOG.isDebugEnabled()) {
90        LOG.debug("GET " + uriInfo.getAbsolutePath());
91      }
92      servlet.getMetrics().incrementRequests(1);
93      try {
94        ResponseBuilder response =
95          Response.ok(new TableSchemaModel(getTableSchema()));
96        response.cacheControl(cacheControl);
97        servlet.getMetrics().incrementSucessfulGetRequests(1);
98        return response.build();
99      } catch (TableNotFoundException e) {
100       servlet.getMetrics().incrementFailedGetRequests(1);
101       return Response.status(Response.Status.NOT_FOUND)
102         .type(MIMETYPE_TEXT).entity("Not found" + CRLF)
103         .build();
104     } catch (IOException e) {
105       servlet.getMetrics().incrementFailedGetRequests(1);
106       return Response.status(Response.Status.SERVICE_UNAVAILABLE)
107         .type(MIMETYPE_TEXT).entity("Unavailable" + CRLF)
108         .build();
109     }
110   }
111 
112   private Response replace(final byte[] name, final TableSchemaModel model,
113       final UriInfo uriInfo, final HBaseAdmin admin) {
114     if (servlet.isReadOnly()) {
115       return Response.status(Response.Status.FORBIDDEN)
116         .type(MIMETYPE_TEXT).entity("Forbidden" + CRLF)
117         .build();
118     }
119     try {
120       HTableDescriptor htd = new HTableDescriptor(TableName.valueOf(name));
121       for (Map.Entry<QName,Object> e: model.getAny().entrySet()) {
122         htd.setValue(e.getKey().getLocalPart(), e.getValue().toString());
123       }
124       for (ColumnSchemaModel family: model.getColumns()) {
125         HColumnDescriptor hcd = new HColumnDescriptor(family.getName());
126         for (Map.Entry<QName,Object> e: family.getAny().entrySet()) {
127           hcd.setValue(e.getKey().getLocalPart(), e.getValue().toString());
128         }
129         htd.addFamily(hcd);
130       }
131       if (admin.tableExists(name)) {
132         admin.disableTable(name);
133         admin.modifyTable(name, htd);
134         admin.enableTable(name);
135         servlet.getMetrics().incrementSucessfulPutRequests(1);
136       } else try {
137         admin.createTable(htd);
138         servlet.getMetrics().incrementSucessfulPutRequests(1);
139       } catch (TableExistsException e) {
140         // race, someone else created a table with the same name
141         return Response.status(Response.Status.NOT_MODIFIED)
142           .type(MIMETYPE_TEXT).entity("Not modified" + CRLF)
143           .build();
144       }
145       return Response.created(uriInfo.getAbsolutePath()).build();
146     } catch (IOException e) {
147       return Response.status(Response.Status.SERVICE_UNAVAILABLE)
148         .type(MIMETYPE_TEXT).entity("Unavailable" + CRLF)
149         .build();
150     }
151   }
152 
153   private Response update(final byte[] name, final TableSchemaModel model,
154       final UriInfo uriInfo, final HBaseAdmin admin) {
155     if (servlet.isReadOnly()) {
156       return Response.status(Response.Status.FORBIDDEN)
157         .type(MIMETYPE_TEXT).entity("Forbidden" + CRLF)
158         .build();
159     }
160     try {
161       HTableDescriptor htd = admin.getTableDescriptor(name);
162       admin.disableTable(name);
163       try {
164         for (ColumnSchemaModel family: model.getColumns()) {
165           HColumnDescriptor hcd = new HColumnDescriptor(family.getName());
166           for (Map.Entry<QName,Object> e: family.getAny().entrySet()) {
167             hcd.setValue(e.getKey().getLocalPart(), e.getValue().toString());
168           }
169           if (htd.hasFamily(hcd.getName())) {
170             admin.modifyColumn(name, hcd);
171           } else {
172             admin.addColumn(name, hcd);
173           }
174         }
175       } catch (IOException e) {
176         return Response.status(Response.Status.SERVICE_UNAVAILABLE)
177           .type(MIMETYPE_TEXT).entity("Unavailable" + CRLF)
178           .build();
179       } finally {
180         admin.enableTable(tableResource.getName());
181       }
182       servlet.getMetrics().incrementSucessfulPutRequests(1);
183       return Response.ok().build();
184     } catch (IOException e) {
185       return Response.status(Response.Status.SERVICE_UNAVAILABLE)
186         .type(MIMETYPE_TEXT).entity("Unavailable" + CRLF)
187         .build();
188     }
189   }
190 
191   private Response update(final TableSchemaModel model, final boolean replace,
192       final UriInfo uriInfo) {
193     try {
194       byte[] name = Bytes.toBytes(tableResource.getName());
195       HBaseAdmin admin = servlet.getAdmin();
196       if (replace || !admin.tableExists(name)) {
197         return replace(name, model, uriInfo, admin);
198       } else {
199         return update(name, model, uriInfo, admin);
200       }
201     } catch (IOException e) {
202       servlet.getMetrics().incrementFailedPutRequests(1);
203       return Response.status(Response.Status.SERVICE_UNAVAILABLE)
204         .type(MIMETYPE_TEXT).entity("Unavailable" + CRLF)
205         .build();
206     }
207   }
208 
209   @PUT
210   @Consumes({MIMETYPE_XML, MIMETYPE_JSON, MIMETYPE_PROTOBUF,
211     MIMETYPE_PROTOBUF_IETF})
212   public Response put(final TableSchemaModel model, 
213       final @Context UriInfo uriInfo) {
214     if (LOG.isDebugEnabled()) {
215       LOG.debug("PUT " + uriInfo.getAbsolutePath());
216     }
217     servlet.getMetrics().incrementRequests(1);
218     return update(model, true, uriInfo);
219   }
220 
221   @POST
222   @Consumes({MIMETYPE_XML, MIMETYPE_JSON, MIMETYPE_PROTOBUF,
223     MIMETYPE_PROTOBUF_IETF})
224   public Response post(final TableSchemaModel model, 
225       final @Context UriInfo uriInfo) {
226     if (LOG.isDebugEnabled()) {
227       LOG.debug("PUT " + uriInfo.getAbsolutePath());
228     }
229     servlet.getMetrics().incrementRequests(1);
230     return update(model, false, uriInfo);
231   }
232 
233   @DELETE
234   public Response delete(final @Context UriInfo uriInfo) {
235     if (LOG.isDebugEnabled()) {
236       LOG.debug("DELETE " + uriInfo.getAbsolutePath());
237     }
238     servlet.getMetrics().incrementRequests(1);
239     if (servlet.isReadOnly()) {
240       return Response.status(Response.Status.FORBIDDEN).type(MIMETYPE_TEXT)
241           .entity("Forbidden" + CRLF).build();
242     }
243     try {
244       HBaseAdmin admin = servlet.getAdmin();
245       boolean success = false;
246       for (int i = 0; i < 10; i++) try {
247         admin.disableTable(tableResource.getName());
248         success = true;
249         break;
250       } catch (IOException e) {
251       }
252       if (!success) {
253         throw new IOException("could not disable table");
254       }
255       admin.deleteTable(tableResource.getName());
256       servlet.getMetrics().incrementSucessfulDeleteRequests(1);
257       return Response.ok().build();
258     } catch (TableNotFoundException e) {
259       servlet.getMetrics().incrementFailedDeleteRequests(1);
260       return Response.status(Response.Status.NOT_FOUND)
261         .type(MIMETYPE_TEXT).entity("Not found" + CRLF)
262         .build();
263     } catch (IOException e) {
264       servlet.getMetrics().incrementFailedDeleteRequests(1);
265       return Response.status(Response.Status.SERVICE_UNAVAILABLE)
266         .type(MIMETYPE_TEXT).entity("Unavailable" + CRLF)
267         .build();
268     }
269   }
270 }