Skip to content

Latest commit

 

History

History
241 lines (187 loc) · 7.29 KB

File metadata and controls

241 lines (187 loc) · 7.29 KB

MATLAB HTTP API Migration Summary

Overview

Successfully migrated from MATLAB Engine API (direct Python-MATLAB integration) to HTTP-based microservice architecture.

Architecture Change

Before (MATLAB Engine API)

  • Web container attempted to install MATLAB Engine API Python package
  • Direct Python-to-MATLAB connection via matlab.engine
  • Complex dependency management and version compatibility issues
  • Required MATLAB Engine compiled libraries in web container

After (HTTP API)

  • MATLAB container runs Flask HTTP API server on port 9090
  • Web container makes HTTP requests to MATLAB container
  • Clean separation: MATLAB stays fully in MATLAB container
  • No MATLAB dependencies in web container

Files Created

1. /docker/matlab/matlab_http_server.py (NEW)

Purpose: Flask HTTP API server running in MATLAB container

Endpoints:

  • GET /health - Health check endpoint for Docker healthcheck
  • POST /execute - Execute MATLAB functions with args/kwargs
  • POST /eval - Evaluate MATLAB code strings

Features:

  • Initializes MATLAB engine on startup
  • Adds MATLAB paths and initializes COBRA Toolbox
  • Converts MATLAB results to Python-compatible JSON
  • Comprehensive error handling with 300s timeout
  • Graceful shutdown handling

2. /curationTool/reactions/utils/MatlabHTTPClient.py (NEW)

Purpose: HTTP client for web container to communicate with MATLAB API

Key Methods:

  • execute(function_name, *args, **kwargs) - Call MATLAB functions
  • eval(matlab_code) - Evaluate MATLAB code
  • health_check() - Verify MATLAB server availability

Features:

  • Singleton pattern for connection pooling
  • 300s timeout for long-running MATLAB operations
  • Automatic retry logic
  • Connection error handling
  • Backwards compatible alias: MatlabSessionManager = MatlabHTTPClient

Files Modified

3. /Dockerfile (UPDATED)

Changes:

  • ✅ Removed entire matlab-builder stage
  • ✅ Removed all MATLAB Engine API installation steps
  • ✅ Removed COPY commands from matlab-builder
  • ✅ Simplified to single-stage build with Python 3.11-slim
  • ✅ No MATLAB dependencies in web container

4. /docker/matlab/Dockerfile (UPDATED)

Changes:

  • ✅ Added Flask installation: pip3 install --break-system-packages flask
  • ✅ Changed CMD from matlab_engine_server.py to matlab_http_server.py
  • ✅ MATLAB Engine API remains available for Flask server to use

5. /docker-compose.yml (UPDATED)

Web Service Changes:

  • ✅ Removed: MATLAB_REMOTE_ENABLED, MATLAB_SESSION_NAME
  • ✅ Added: MATLAB_HOST=matlab, MATLAB_HTTP_PORT=9090

MATLAB Service Changes:

  • ✅ Updated healthcheck from Python MATLAB Engine test to:
    test: ["CMD", "curl", "-f", "http://localhost:9090/health", "||", "exit", "1"]
  • ✅ Flask server starts on port 9090

6. /curationTool/reactions/utils/gen_vmh_abbrs.py (UPDATED)

Changes:

  • ✅ Simplified import: from reactions.utils.MatlabHTTPClient import MatlabSessionManager
  • ✅ Removed 80+ lines of MATLAB Engine import logic
  • ✅ Removed all debug logging
  • ✅ Uses HTTP client transparently

7. /curationTool/reactions/views/vmh_views.py (UPDATED)

Changes:

  • ✅ Simplified import logic
  • ✅ Removed conditional import based on MATLAB_REMOTE_ENABLED
  • ✅ Now only imports: from reactions.utils.MatlabHTTPClient import MatlabSessionManager
  • ✅ All existing code using MatlabSessionManager() works unchanged

8. /docker/test_matlab_connection.py (UPDATED)

Changes:

  • ✅ Updated to use MatlabHTTPClient
  • ✅ Changed environment variable checks to MATLAB_HOST and MATLAB_HTTP_PORT
  • ✅ Test suite remains compatible with .execute() interface

Application Code Compatibility

✅ No Changes Required

The following files continue to work without modification:

  • /curationTool/reactions/utils/add_to_vmh_utils.py
    • add_reaction_matlab() - Uses matlab_session.execute('add_rxn_python', ...)
    • add_metabolites_matlab() - Uses matlab_session.execute('add_metab_python', ...)

All application code using the .execute() interface remains 100% compatible.

Deprecated Files (Not Deleted)

These files are no longer used but kept for reference:

  • /curationTool/reactions/utils/MatlabSessionManager.py - Old local MATLAB Engine API
  • /curationTool/reactions/utils/MatlabSessionManagerRemote.py - Old remote MATLAB Engine API
  • /docker/matlab/matlab_engine_server.py - Old MATLAB Engine sharing server

Note: These files won't be imported anymore and can be safely deleted after verifying the new system works.

Environment Variables

Old (Removed)

MATLAB_REMOTE_ENABLED=true
MATLAB_SESSION_NAME=matlab_shared_session

New (Active)

MATLAB_HOST=matlab                    # Docker service name
MATLAB_HTTP_PORT=9090                 # Flask API port

API Usage Examples

Execute MATLAB Function

from reactions.utils.MatlabHTTPClient import MatlabSessionManager

matlab = MatlabSessionManager()
result = matlab.execute('generateVMHMetAbbr', 'glucose')

if result['status'] == 'success':
    abbr = result['result']
    print(f"Generated abbreviation: {abbr}")
else:
    print(f"Error: {result['message']}")

Evaluate MATLAB Code

result = matlab.eval('2 + 2')
# Returns: {'status': 'success', 'result': 4.0}

Health Check

is_healthy = matlab.health_check()
# Returns: True if MATLAB server is responsive

Testing Plan

1. Rebuild Docker Images

sudo docker compose build

2. Start Services

sudo docker compose up -d

3. Verify MATLAB HTTP Server

# Check MATLAB container logs
sudo docker compose logs matlab

# Should see: "Starting MATLAB HTTP API server on port 9090"
# Should see: "MATLAB HTTP API server is ready on http://0.0.0.0:9090"

4. Test Health Endpoint

# From host
curl http://localhost:9090/health

# Should return: {"status": "healthy", "matlab": "initialized"}

5. Test Django Application

# Run the test script
sudo docker compose exec web python /app/docker/test_matlab_connection.py

# Or access the web interface and test:
# - Generate VMH abbreviations
# - Add metabolites/reactions to VMH

Benefits of HTTP Approach

✅ Simplicity

  • No complex Python version matching
  • No compiled library dependencies
  • Clear container boundaries

✅ Maintainability

  • Easy to debug with curl/HTTP tools
  • Clear API contracts
  • Independent deployment and scaling

✅ Reliability

  • HTTP timeouts prevent hanging
  • Health checks for monitoring
  • Graceful error handling

✅ Flexibility

  • Can be called from any language
  • Easy to add new endpoints
  • Simple to extend functionality

Rollback Plan

If issues occur, environment variables can be temporarily adjusted, but the old approach is deprecated. The HTTP API is the recommended production solution.

Next Steps

  1. ✅ All code updated
  2. ⏳ Rebuild Docker images
  3. ⏳ Test HTTP connectivity
  4. ⏳ Verify MATLAB function execution
  5. ⏳ Test full application workflow
  6. ⏳ Delete deprecated files after verification

Support

For issues or questions:

  1. Check MATLAB container logs: sudo docker compose logs matlab
  2. Check web container logs: sudo docker compose logs web
  3. Test HTTP endpoint directly: curl http://localhost:9090/health
  4. Verify environment variables are set correctly