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

HotelsDotCom / waggle-dance / #361

20 Oct 2023 03:42PM UTC coverage: 73.43% (+0.01%) from 73.418%
#361

push

web-flow
Added timeout on isAvailable calls (#300)

* Added timeout

* Update CHANGELOG.md

16 of 16 new or added lines in 1 file covered. (100.0%)

2385 of 3248 relevant lines covered (73.43%)

0.73 hits per line

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

95.83
/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.Future;
25
import java.util.concurrent.TimeUnit;
26
import java.util.concurrent.TimeoutException;
27

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

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

43
class MetaStoreMappingImpl implements MetaStoreMapping {
44

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

47
  // MilliSeconds
48
  static final long DEFAULT_AVAILABILITY_TIMEOUT = 500;
1✔
49

50
  private final String databasePrefix;
51
  private final CloseableThriftHiveMetastoreIface client;
52
  private final AccessControlHandler accessControlHandler;
53
  private final String name;
54
  private final long latency;
55
  private final MetaStoreFilterHook metastoreFilter;
56

57
  private final ConnectionType connectionType;
58

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

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

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

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

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

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

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

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

112
  @Override
113
  public void close() throws IOException {
114
    client.close();
1✔
115
  }
1✔
116

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

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

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

165
  @Override
166
  public String getMetastoreMappingName() {
167
    return name;
1✔
168
  }
169

170
  @Override
171
  public long getLatency() {
172
    return latency;
1✔
173
  }
174

175
}
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