Revision 6368bb51

View differences:

include/MarketPlaceAppPool.h
73 73
     *    @param template to generate app with the from_template64 function
74 74
     *    @param mp_id of the MarketPlace to store de App
75 75
     *    @param mp_name of the MarketPlace
76
     *    @param app_id of the imported app
76 77
     *    @param error_str Returns the error reason, if any
77 78
     *
78 79
     *    @return the oid assigned to the object, -1 in case of failure, -2
79 80
     *    already imported
80 81
     */
81 82
    int import(const std::string& t64, int mp_id, const std::string& mp_name,
82
            std::string& error_str);
83
            int& app_id, std::string& error_str);
83 84

  
84 85
    /**
85 86
     *  Function to get a MarketPlaceApp from the pool
......
147 148
    };
148 149

  
149 150
    /**
150
     * Erease map element
151
     * Check an element into map
152
     *   @param map_id of the app
153
     *   @return true if the app has to be deleted
151 154
     */
152
    void drop_map_check(const std::string& name){
153
        if (map_check.find( name ) != map_check.end()){
154
            map<std::string,int>::iterator it;
155
            it=map_check.find(name);
156
            map_check.erase (it);
157
        }
158
    }
155
    bool test_map_check(int map_id);
159 156

  
160 157
    /**
161
     * Check an element into map
158
     *  Resets the counter of missing monitors of an app
159
     *    @param app_id of the app
162 160
     */
163
    bool test_map_check(const std::string& name){
164
        map_check[name]++;
165
        if (map_check[name] > 0){
166
            return true;
167
        }
168
        return false;
169
    }
161
    void reset_map_check(int app_id);
162

  
170 163
private:
171
    map<std::string, int> map_check;
172

  
173
    
174
    void insert_map_check(const std::string& name){
175
        map_check.insert(make_pair(name, -1));
176
    }
177
    void reset_map_check(const std::string& name){
178
        if (name != "") {
179
            if (map_check.find( name ) != map_check.end()){
180
                map_check[name] = -1;
181
            }
182
            else{
183
                insert_map_check(name);
184
            }
185
        }
186
    }
164

  
165
    /**
166
     *  Hash to store the number of times an app was missing from monitor data
167
     */
168
    map<int, int> map_check;
169

  
170
    /**
171
     *  Max number of monitor that an app may be missing before deleting it
172
     */
173
    static const int MAX_MISSING_MONITORS;
187 174
};
188 175

  
189 176
#endif /*MARKETPLACE_POOL_H_*/
src/market/MarketPlaceAppPool.cc
128 128

  
129 129
    *oid = PoolSQL::allocate(mp, error_str);
130 130

  
131
    // ------------------------------------------------------------------------
132
    // Insert id into map_check
133
    // --------------------------------------------------------------------------
134
    insert_map_check(name);
135

  
136 131
    return *oid;
137 132

  
138 133
error_duplicated:
......
188 183
        return 0;
189 184
    }
190 185

  
191
    drop_map_check(objsql->get_name());
192

  
193 186
    return PoolSQL::drop(objsql, error_msg);
194 187
}
195 188

  
......
197 190
/* -------------------------------------------------------------------------- */
198 191

  
199 192
int MarketPlaceAppPool::import(const std::string& t64, int mp_id,
200
        const std::string& mp_name, std::string& error_str)
193
        const std::string& mp_name, int& app_id, std::string& error_str)
201 194
{
202 195
    // -------------------------------------------------------------------------
203 196
    // Build the marketplace app object
......
236 229

  
237 230
    if( mp_aux != 0 ) //Marketplace app already imported
238 231
    {
239
        reset_map_check(app->name);
232
        app_id = mp_aux->oid;
233

  
240 234
        if ( mp_aux->version != app->version || mp_aux->md5 != app->md5 )
241 235
        {
242 236
            mp_aux->from_template64(t64, error_str);
......
256 250
    // -------------------------------------------------------------------------
257 251
    if (Nebula::instance().is_federation_slave())
258 252
    {
259
        int oid = master_allocate(app, error_str);
253
        app_id = master_allocate(app, error_str);
260 254

  
261 255
        app->lock();
262 256
        delete app;
263 257

  
264
        return oid;
258
        return app_id;
265 259
    }
266
    insert_map_check(app->name);
267
    return PoolSQL::allocate(app, error_str);
260

  
261
    app_id = PoolSQL::allocate(app, error_str);
262

  
263
    return app_id;
268 264
}
269 265

  
270 266
/* -------------------------------------------------------------------------- */
......
316 312

  
317 313
/* -------------------------------------------------------------------------- */
318 314
/* -------------------------------------------------------------------------- */
315
const int MarketPlaceAppPool::MAX_MISSING_MONITORS = 3;
316

  
317
bool MarketPlaceAppPool::test_map_check(int app_id)
318
{
319
    map<int, int>::iterator it = map_check.find(app_id);
320

  
321
    if ( it == map_check.end() )
322
    {
323
        return false;
324
    }
325

  
326
    it->second++;
327

  
328
    bool to_delete = it->second >= MAX_MISSING_MONITORS;
319 329

  
330
    if ( to_delete )
331
    {
332
        map_check.erase(it); 
333
    }
334

  
335
    return to_delete;
336
}
337

  
338
void MarketPlaceAppPool::reset_map_check(int app_id)
339
{
340
    map<int, int>::iterator it = map_check.find(app_id);
341

  
342
    if ( it == map_check.end() )
343
    {
344
        map_check.insert(make_pair(app_id, -1));
345
    }
346
    else
347
    {
348
        it->second = -1;
349
    }
350
}
src/market/MarketPlaceManagerDriver.cc
120 120
        return;
121 121
    }
122 122

  
123
    std::string   name;
124 123
    MarketPlace * market = marketpool->get(id, true);
125 124

  
126 125
    if (market == 0 )
......
128 127
        return;
129 128
    }
130 129

  
131
    name    = market->get_name();
130
    set<int> apps_mp = market->get_marketapp_ids();
131
    std::string name = market->get_name();
132 132

  
133 133
    market->update_monitor(monitor_data);
134 134

  
......
143 143

  
144 144
    for (int i=0; i< num ; i++)
145 145
    {
146
        int rc = apppool->import(apps[i]->value(), id, name, err);
146
        int app_id;
147
        int rc = apppool->import(apps[i]->value(), id, name, app_id, err);
147 148

  
148 149
        if ( rc == -1 )
149 150
        {
......
162 163
                market->unlock();
163 164
            }
164 165
        }
165
    }
166
    MarketPlaceApp *mp_app = nullptr;
167
    std::string error;
168
    std::string source;
169
    int rc_del;
170
    market = marketpool->get(id, true);
171
    set<int> apps_mp = market->get_marketapp_ids();
172
    market->unlock();
173 166

  
174
    for (set<int>::iterator i = apps_mp.begin(); i != apps_mp.end(); i++) {
175
        mp_app = apppool->get(*i, true);
176
        if ( mp_app != 0 )
177
        {
178
            if(apppool->test_map_check(mp_app->get_name())){ //delete app
179
                market = marketpool->get(id, true);
167
        apppool->reset_map_check(app_id);
180 168

  
181
                source = mp_app->get_source();
182
                rc_del = apppool->drop(mp_app, error);
169
        apps_mp.erase(app_id);
170
    }
183 171

  
184
                market->del_marketapp(*i);
185
                marketpool->update(market);
172
    for (set<int>::iterator i = apps_mp.begin(); i != apps_mp.end(); ++i)
173
    {
174
        if (apppool->test_map_check(*i)) //delete app
175
        { 
176
            std::string error;
186 177

  
187
                market->unlock();
188
                if ( rc_del < 0 )
189
                {
190
                    oss << " Error removing app from DB: " << error
191
                        << ". Remove app manually, source is: " << source;
192
                }
178
            MarketPlaceApp * app = apppool->get(*i, true);
179

  
180
            if ( app == 0 )
181
            {
182
                continue;
193 183
            }
184

  
185
            rc = apppool->drop(app, error);
186

  
187
            app->unlock();
188

  
189
            market = marketpool->get(id, true);
190

  
191
            market->del_marketapp(*i);
192

  
193
            marketpool->update(market);
194

  
195
            market->unlock();
194 196
        }
195
        mp_app->unlock();
196 197
    }
197 198

  
198 199
    oss << "Marketplace " << name << " (" << id << ") successfully monitored.";

Also available in: Unified diff