• Home
  • Features
  • Pricing
  • Docs
  • Announcements
  • Sign In

HotelsDotCom / waggle-dance / #368

24 Oct 2023 02:07PM UTC coverage: 73.44% (+0.01%) from 73.43%
#368

push

eg-oss-ci
[maven-release-plugin] prepare for next development iteration

2389 of 3253 relevant lines covered (73.44%)

0.73 hits per line

Source File
Press 'n' to go to next uncovered line, 'b' for previous

96.15
/waggle-dance-core/src/main/java/com/hotels/bdp/waggledance/mapping/model/MetaStoreMappingImpl.java
1
/**
2
 * Copyright (C) 2016-2023 Expedia, Inc.
3
 *
4
 * Licensed under the Apache License, Version 2.0 (the "License");
5
 * you may not use this file except in compliance with the License.
6
 * You may obtain a copy of the License at
7
 *
8
 * http://www.apache.org/licenses/LICENSE-2.0
9
 *
10
 * Unless required by applicable law or agreed to in writing, software
11
 * distributed under the License is distributed on an "AS IS" BASIS,
12
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13
 * See the License for the specific language governing permissions and
14
 * limitations under the License.
15
 */
16
package com.hotels.bdp.waggledance.mapping.model;
17

18
import java.io.IOException;
19
import java.util.Collections;
20
import java.util.List;
21
import java.util.Locale;
22
import java.util.concurrent.CompletableFuture;
23
import java.util.concurrent.ExecutionException;
24
import java.util.concurrent.ExecutorService;
25
import java.util.concurrent.Executors;
26
import java.util.concurrent.Future;
27
import java.util.concurrent.TimeUnit;
28
import java.util.concurrent.TimeoutException;
29

30
import org.apache.hadoop.hive.metastore.MetaStoreFilterHook;
31
import org.apache.hadoop.hive.metastore.api.AlreadyExistsException;
32
import org.apache.hadoop.hive.metastore.api.Database;
33
import org.apache.hadoop.hive.metastore.api.InvalidObjectException;
34
import org.apache.hadoop.hive.metastore.api.MetaException;
35
import org.apache.hadoop.hive.metastore.api.ThriftHiveMetastore;
36
import org.apache.thrift.TException;
37
import org.slf4j.Logger;
38
import org.slf4j.LoggerFactory;
39

40
import com.hotels.bdp.waggledance.api.model.ConnectionType;
41
import com.hotels.bdp.waggledance.client.CloseableThriftHiveMetastoreIface;
42
import com.hotels.bdp.waggledance.server.security.AccessControlHandler;
43
import com.hotels.bdp.waggledance.server.security.NotAllowedException;
44

45
class MetaStoreMappingImpl implements MetaStoreMapping {
46

47
  private final static Logger log = LoggerFactory.getLogger(MetaStoreMappingImpl.class);
1✔
48

49
  // MilliSeconds
50
  static final long DEFAULT_AVAILABILITY_TIMEOUT = 2000;
1✔
51

52
  private final String databasePrefix;
53
  private final CloseableThriftHiveMetastoreIface client;
54
  private final AccessControlHandler accessControlHandler;
55
  private final String name;
56
  private final long latency;
57
  private final MetaStoreFilterHook metastoreFilter;
58
  private final ConnectionType connectionType;
59
  private final ExecutorService executor = Executors.newSingleThreadExecutor();
1✔
60

61
  MetaStoreMappingImpl(
1✔
62
      String databasePrefix,
63
      String name,
64
      CloseableThriftHiveMetastoreIface client,
65
      AccessControlHandler accessControlHandler,
66
      ConnectionType connectionType,
67
      long latency,
68
      MetaStoreFilterHook metastoreFilter) {
69
    this.databasePrefix = databasePrefix;
1✔
70
    this.name = name;
1✔
71
    this.client = client;
1✔
72
    this.accessControlHandler = accessControlHandler;
1✔
73
    this.connectionType = connectionType;
1✔
74
    this.latency = latency;
1✔
75
    this.metastoreFilter = metastoreFilter;
1✔
76
  }
1✔
77

78
  @Override
79
  public String transformOutboundDatabaseName(String databaseName) {
80
    return databaseName.toLowerCase(Locale.ROOT);
1✔
81
  }
82

83
  @Override
84
  public List<String> transformOutboundDatabaseNameMultiple(String databaseName) {
85
    return Collections.singletonList(transformInboundDatabaseName(databaseName));
1✔
86
  }
87

88
  @Override
89
  public ThriftHiveMetastore.Iface getClient() {
90
    return client;
1✔
91
  }
92

93
  @Override
94
  public MetaStoreFilterHook getMetastoreFilter() {
95
    return metastoreFilter;
1✔
96
  }
97

98
  @Override
99
  public Database transformOutboundDatabase(Database database) {
100
    database.setName(transformOutboundDatabaseName(database.getName()));
1✔
101
    return database;
1✔
102
  }
103

104
  @Override
105
  public String transformInboundDatabaseName(String databaseName) {
106
    return databaseName.toLowerCase(Locale.ROOT);
1✔
107
  }
108

109
  @Override
110
  public String getDatabasePrefix() {
111
    return databasePrefix;
1✔
112
  }
113

114
  @Override
115
  public void close() throws IOException {
116
    client.close();
1✔
117
    executor.shutdownNow();
1✔
118
  }
1✔
119

120
  /**
121
   * This is potentially slow so a best effort is made and false is returned after a timeout.
122
   */
123
  @Override
124
  public boolean isAvailable() {
125
    Future<Boolean> future = CompletableFuture.supplyAsync(() -> {
1✔
126
      try {
127
        boolean isOpen = client.isOpen();
1✔
128
        if (isOpen && connectionType == ConnectionType.TUNNELED) {
1✔
129
          client.getStatus();
1✔
130
        }
131
        return isOpen;
1✔
132
      } catch (Exception e) {
1✔
133
        log.error("Metastore Mapping {} unavailable", name, e);
1✔
134
        return false;
1✔
135
      }
136
    }, executor);
1✔
137
    long timeout = DEFAULT_AVAILABILITY_TIMEOUT + getLatency();
1✔
138
    try {
139
      return future.get(timeout, TimeUnit.MILLISECONDS);
1✔
140
    } catch (TimeoutException e) {
1✔
141
      log.info("Took too long (>" + timeout + "ms) to check availability of '" + name + "', assuming unavailable");
1✔
142
      future.cancel(true);
1✔
143
    } catch (InterruptedException | ExecutionException e) {
×
144
      log.error("Error while checking availability '" + name + "', assuming unavailable");
×
145
    }
146
    return false;
1✔
147
  }
148

149
  @Override
150
  public MetaStoreMapping checkWritePermissions(String databaseName) {
151
    if (!accessControlHandler.hasWritePermission(databaseName)) {
1✔
152
      throw new NotAllowedException(
1✔
153
          "You cannot perform this operation on the virtual database '" + databaseName + "'.");
1✔
154
    }
155
    return this;
1✔
156
  }
157

158
  @Override
159
  public void createDatabase(Database database)
160
    throws AlreadyExistsException, InvalidObjectException, MetaException, TException {
161
    if (accessControlHandler.hasCreatePermission()) {
1✔
162
      getClient().create_database(database);
1✔
163
      accessControlHandler.databaseCreatedNotification(database.getName());
1✔
164
    } else {
1✔
165
      throw new NotAllowedException("You cannot create the database '" + database.getName() + "'.");
1✔
166
    }
167
  }
1✔
168

169
  @Override
170
  public String getMetastoreMappingName() {
171
    return name;
1✔
172
  }
173

174
  @Override
175
  public long getLatency() {
176
    return latency;
1✔
177
  }
178

179
}
STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2026 Coveralls, Inc